java代码 如何向TXT文件写入内容?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java代码 如何向TXT文件写入内容?相关的知识,希望对你有一定的参考价值。
比如这样....
按这种格式输入内容...
一次性 写进1000万个内容也可以
0001 null
0002 null
...
..
..
0999 null
就这种格式
怎么写入?
向txt文件写入内容基本思路就是获得一个file对象,新建一个txt文件,打开I/O操作流,使用写入方法进行读写内容,示例如下:
package common;import java.io.*;
import java.util.ArrayList;
public class IOTest
public static void main (String args[])
ReadDate();
WriteDate();
/**
* 读取数据
*/
public static void ReadDate()
String url = “e:/2.txt”;
try
FileReader read = new FileReader(new File(url));
StringBuffer sb = new StringBuffer();
char ch[] = new char[1024];
int d = read.read(ch);
while(d!=-1)
String str = new String(ch,0,d);
sb.append(str);
d = read.read(ch);
System.out.print(sb.toString());
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
/**
* 写入数据
*/
public static void WriteDate()
try
File file = new File(“D:/abc.txt”);
if (file.exists())
file.delete();
file.createNewFile();
BufferedWriter output = new BufferedWriter(new FileWriter(file));
ArrayList ResolveList = new ArrayList();
for (int i = 0; i < 10; i++)
ResolveList.add(Math.random()* 100);
for (int i=0 ;i
output.write(String.valueOf(ResolveList.get(i)) + “\\n”);
output.close();
catch (Exception ex)
System.out.println(ex);
原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/database/303/12439303.shtml 参考技术A import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static final int MAX = 10000;
public static final String FILE_PATH = "c:\\\\content.txt";
public static String[] contents = new String[MAX];
static
for (int i = 0; i < 10000; i++)
contents[i] = "" + i;
public static void main(String[] args)
FileWriter fw = null;
try
File f = new File(FILE_PATH);
if (!f.exists())
f.createNewFile();
fw = new FileWriter(FILE_PATH, true);
for (int i = 0; i < 10000; i++)
String index = "";
int indexLength = String.valueOf(i).length();
for (int j = 0; j < String.valueOf(MAX).length() - indexLength; j++)
index += "0";
index += String.valueOf(i);
fw.write(index + "\\t" + contents[i] + LINE_SEPARATOR);
catch (Exception e)
System.out.println(e.toString());
finally
if (fw != null)
try
fw.close();
catch (IOException e)
throw new RuntimeException("Close Failed");
本回答被提问者采纳 参考技术B 用for循环,每次写入一行追问
求代码
我是新手 0.0
不是, 是自己创建的txt文件
java 如何向txt文件中的某一行继续写入
打开一个已存在的txt文件,每一行是一条记录。
想对已知的某一行进行追加写入,如,在第五行继续写入一个字符串,有什么思路?
最好有具体的代码例子。
问题的关键不在于追加,在于向特定的某一行写入。
比如文件有10行啊~我只想在第五行接着写。
Java的RandomAccessFile提供对文件的读写功能,与普通的输入输出流不一样的是RamdomAccessFile可以任意的访问文件的任何地方。这就是“Random”的意义所在。
相关API:
RandomAccessFile(String
name, String
mode)构造器,模式分为r(只读),rw(读写)等
RandomAccessFile.readLine()方法实现对一整行的读取,并重新定位操作位置
RandomAccessFile.write(byte[] b)用于字节内容的写入
示例如下:
RandomAccessFile raf = new RandomAccessFile("f:/1.txt", "rw");int targetLineNum = 10;
int currentLineNum = 0;
while(raf.readLine() != null)
if(currentLineNum == targetLineNum) // 定位到目标行时结束
break;
currentLineNum++;
raf.write("\\r\\ninsert".getBytes());
raf.close();
根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象
你仔细看看构造方法,有一个就是你象要的,比如上面的
你可以先查找要插入的位置,然后用
write
public void write(String str,
int off,
int len)
throws IOException写入字符串的某一部分。
参数:
str - 字符串
off - 相对初始写入字符的偏移量
len - 要写入的字符数
这个方法 参考技术B 可以将BufferedWriter流和FileWriter流连接在一起,然后使用BufferedWriter将流数据写到目的地。
例如:
FileWriter tofile=new FileWriter("Student.txt");
BufferedWriter out=new BufferedWriter(tofile); 参考技术C 用RandomAccessFile不行吗?
以上是关于java代码 如何向TXT文件写入内容?的主要内容,如果未能解决你的问题,请参考以下文章