英文:
How to write the content Line by Line in a file using java
问题
以下是翻译好的代码部分:
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", true); // 追加模式
fo.write((random_no + "\n").getBytes()); // 写入并换行
}
fo.close();
System.out.println("Size is : " + newset.size());
}
请注意,我在FileOutputStream
的构造函数中添加了参数true
,以便在每次写入时都以追加模式打开文件,并且我在fo.write
中添加了\n
来在每次写入后换行。这样可以确保每个随机数都会被逐行写入到文本文件中。
英文:
How do i write all the ouputs line by line in a txt file? the below snippet is writing only 1 line. Basically i want random_no to be written line by line in txt file.
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());
答案1
得分: 1
public static void main(String[] args) throws IOException {
FileOutputStream fo = new FileOutputStream("D:\Test_No.txt");
HashSet
for (int i = 0; i < 100; i++) {
double d = Math.random() * 10000;
String random_no = Integer.toString((int) d);
newset.add((int)d);
fo.write(random_no.getBytes());
fo.write("\n".getBytes());
}
fo.close();
}
英文:
Here you need to do two things:
a) Open file outside the loop
b) Write newline after every number
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();
}
答案2
得分: 0
你在循环内部打开文件,尝试:
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());
}
你还可以考虑自动处理关闭:
public static void main(String args[]) throws FileNotFoundException, IOException {
HashSet<Integer> newset = new HashSet<Integer>();
try (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());
}
}
System.out.println("Size is: " + newset.size());
}
英文:
You're opening the file inside the loop, try:
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());
}
You might also want to consider handling the close automatically:
public static void main(String args[]) throws FileNotFoundException, IOException {
HashSet <Integer> newset = new HashSet <Integer>();
try (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());
}
}
System.out.println("Size is : " +newset.size());
}
答案3
得分: 0
`FileOutputStream` 应该在循环之外进行初始化。
此外,要打印新行,您还可以使用 `BufferedWriter` 而不是 `FileOutputStream`。
`BufferedWriter` 提供了 `BufferedWriter.newLine()` 选项,该选项可以独立于环境(Windows、Unix 等)处理换行。
```java
public static void main(String args[]) throws IOException {
HashSet<Integer> newset = new HashSet<>();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("Test_No.txt", true));
for (int i = 0; i < 100; i++) {
double d = Math.random() * 10000;
String random_no = Integer.toString((int) d);
newset.add((int) d);
bufferedWriter.write(random_no);
bufferedWriter.newLine();
}
bufferedWriter.close();
System.out.println("Size is: " + newset.size());
}
英文:
FileOutputStream
should be initialised outside the loop.
Also, to print new line, you can also use BufferedWriter
instead of FileOutputStream
.
BufferedWriter
gives you option of BufferedWriter.newLine()
which would handle new lines independent of the environment(windows, unix, etc.).
public static void main(String args[]) throws IOException {
HashSet<Integer> newset = new HashSet<>();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("Test_No.txt", true));
for (int i = 0; i < 100; i++) {
double d = Math.random() * 10000;
String random_no = Integer.toString((int) d);
newset.add((int) d);
bufferedWriter.write(random_no);
bufferedWriter.newLine();
}
bufferedWriter.close();
System.out.println("Size is : " + newset.size());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论