Answer the question
In order to leave comments, you need to log in
Why is text written to a file in one line in Java?
A Java program was written that searches the source file for the searched word and writes the lines that contain the searched word to another file
. The problem is that the program writes everything in one line when it should be written out line by line. Here is the
package semestralniprace code;
import java.io.*;
import java.util.*;
public class SemestralniPrace {
public static void main(String[]args)throws Exception{
Scanner sc = new Scanner(System.in);
FileReader in = new FileReader("vstup.txt");
BufferedReader inbr = new BufferedReader(in);
System.out.println("Zadejte hledany prvek");
String ret;
ret = sc.nextLine();
FileWriter out = new FileWriter("vystup.txt");
Stringh;
String radek = inbr.readLine();
int i = 0;
while(radek!=null){
if(radek.indexOf(ret) > -1){
i++;
System.out.println(radek);
out.write(radek);
}
radek=inbr.readLine();
}
if (i == 0){
System.out.println("Hledany neexistuje");
}
out.close();
in.close();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question