如何使用java在文件中逐行写入内容
如何在txt文件中逐行写入所有输出?下面的代码段只写了一行。基本上我希望随机_no被写在txt文件中的行如何使用java在文件中逐行写入内容,java,filewriter,fileoutputstream,Java,Filewriter,Fileoutputstream,如何在txt文件中逐行写入所有输出?下面的代码段只写了一行。基本上我希望随机_no被写在txt文件中的行 public static void main(String args[]) throws FileNotFoundException, IOException { HashSet <Integer> newset = new HashSet <Integer>(); FileOutputStream fo = null;
public static void main(String args[]) throws FileNotFoundException, IOException {
HashSet <Integer> newset = new HashSet <Integer>();
FileOutputStream fo = null;
for(int i =0;i<100;i++){
double d=Math.random()*10000;
//System.out.println(d);
String random_no= Integer.toString((int)d);
newset.add((int)d));
fo = new FileOutputStream("E:\\Test\\Files\\Test_No.txt" );
fo.write(random_no.getBytes();
}
fo.close();
System.out.println("Size is : " +newset.size());
publicstaticvoidmain(字符串args[])抛出FileNotFoundException、IOException{
HashSet newset=newhashset();
FileOutputStream fo=null;
对于(int i=0;i要在循环中打开文件,请尝试:
public static void main(String args[]) throws FileNotFoundException, IOException {
HashSet <Integer> newset = new HashSet <Integer>();
FileOutputStream fo = new FileOutputStream("E:\\Test\\Files\\Test_No.txt" );
for(int i =0;i<100;i++){
double d=Math.random()*10000;
//System.out.println(d);
String random_no= Integer.toString((int)d) + System.lineSeparator();
newset.add((int)d);
fo.write(random_no.getBytes());
}
fo.close();
System.out.println("Size is : " +newset.size());
}
publicstaticvoidmain(字符串args[])抛出FileNotFoundException、IOException{
HashSet newset=newhashset();
FileOutputStream fo=新的FileOutputStream(“E:\\Test\\Files\\Test\u No.txt”);
对于(int i=0;i这里您需要做两件事:
a) 在循环外打开文件
b) 在每个数字后面写换行符
public static void main(String[] args) throws IOException {
FileOutputStream fo = new FileOutputStream("D:\\Test_No.txt");
HashSet<Integer> newset = new HashSet <Integer>();
for (int i = 0; i < 100; i++) {
double d = Math.random() * 10000;
// System.out.println(d);
String random_no = Integer.toString((int) d);
newset.add((int)d);
fo.write(random_no.getBytes());
fo.write("\n".getBytes());
}
fo.close();
}
publicstaticvoidmain(字符串[]args)引发IOException{
FileOutputStream fo=新的FileOutputStream(“D:\\Test\u No.txt”);
HashSet newset=newhashset();
对于(int i=0;i<100;i++){
double d=Math.random()*10000;
//系统输出打印ln(d);
字符串random_no=Integer.toString((int)d);
新闻集添加((int)d);
fo.write(random_no.getBytes());
fo.write(“\n”.getBytes());
}
fo.close();
}
文件输出流
应在循环外部初始化
此外,要打印新行,还可以使用BufferedWriter
而不是FileOutputStream
BufferedWriter
提供了BufferedWriter.newLine()
选项,该选项将独立于环境(windows、unix等)处理新行
publicstaticvoidmain(字符串args[])引发IOException{
HashSet newset=新HashSet();
BufferedWriter BufferedWriter=new BufferedWriter(new FileWriter(“Test_No.txt”,true));
对于(int i=0;i<100;i++){
double d=Math.random()*10000;
字符串random_no=Integer.toString((int)d);
新闻集添加((int)d);
bufferedWriter.write(随机编号);
bufferedWriter.newLine();
}
bufferedWriter.close();
System.out.println(“大小为:“+newset.Size());
}
感谢您指出Andy。它会将所有内容写入文件中,但不会以逐行格式写入。有什么方法可以格式化吗?@Cris-123您需要添加换行字符,因此字符串random\u no=Integer.toString((int)d)+“\r\n”如果要编写文本文件,应该使用实现Writer
的类,例如FileWriter
,因为该类接受字符串和char
值,而不仅仅是byte[]
),这意味着不需要调用getBytes()
在Stirng
对象上,您想自己编写。谢谢您的建议。如果我使用FileWriter而不是FileoutputStream/BufferedWriter,会更快吗?特别是当我需要编写大量行时。超过10000行?不,速度几乎相同。缓冲(通过BufferedWriter
或BufferedOutputWriter
)可能会影响速度,但这可以应用于OutputStream
或Writer
。
public static void main(String[] args) throws IOException {
FileOutputStream fo = new FileOutputStream("D:\\Test_No.txt");
HashSet<Integer> newset = new HashSet <Integer>();
for (int i = 0; i < 100; i++) {
double d = Math.random() * 10000;
// System.out.println(d);
String random_no = Integer.toString((int) d);
newset.add((int)d);
fo.write(random_no.getBytes());
fo.write("\n".getBytes());
}
fo.close();
}