considering the following function
private static void GetText(String nodeValue) throws IOException {
if(!file3.exists()) {
file3.createNewFile();
}
FileOutputStream fop=new FileOutputStream(file3,true);
if(nodeValue!=null)
fop.write(nodeValue.getBytes());
fop.flush();
fop.close();
}
what to add to make it to write each time in the next line?
for example i want each words of a given string in a seperate lline for example:
i am mostafa
writes as:
i
am
mostafa
Solution:
in WINDOWS you should write "\r\n"
for a new line.
Or you could use something like the following. Pay attention to "\n":
/**
* Created by mona on 3/26/16.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
public class FileExample {
public static void main (String[] args) throws java.io.IOException {
File newFile = new File("tweet.txt");
FileWriter fileWriter = new FileWriter(newFile);
fileWriter.write("Mona Jalal");
fileWriter.append("\nMona Mona");
fileWriter.close();
FileReader fileReader = new FileReader(newFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
fileReader.close();
bufferedReader.close();
}
}
Files.write(Paths.get(filepath),texttobewrittentofile,StandardOpenOption.APPEND ,StandardOpenOption.CREATE);
This creates a file, if it does not exist If files exists, it is uses the existing file and text is appended If you want everyline to be written to the next line add lineSepartor for newline into file.
String texttobewrittentofile = text + System.lineSeparator();
You could print through a PrintStream.
PrintStream ps = new PrintStream(fop);
ps.println(nodeValue);
ps.close();
the other answers should work. however I wanna mention
from java doc:
FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.
reading your method codes, you are about to write String to the file, what you were doing is convert String to raw bytes, then write so I think using FileWriter is not a bad idea.
And for the newline problem, Writer has method .write(String), which is convenient to use.
just use \r\n for endline if you are using windows operating system.
if(!file3.exists()){
file3.createNewFile();
}
FileOutputStream fop=new FileOutputStream(file3,true);
if(nodeValue!=null) fop.write(nodeValue.getBytes());
fop.write("\n".getBytes());
fop.flush();
fop.close();
You need to add a newline at the end of each write.
Change the lines
if(nodeValue!=null)
fop.write(nodeValue.getBytes());
fop.flush();
to
if(nodeValue!=null) {
fop.write(nodeValue.getBytes());
fop.write(System.getProperty("line.separator").getBytes());
}
fop.flush();
Update to address your edit:
In order to write each word on a different line, you need to split up your input string and then write each word separately.
private static void GetText(String nodeValue) throws IOException {
if(!file3.exists()) {
file3.createNewFile();
}
FileOutputStream fop=new FileOutputStream(file3,true);
if(nodeValue!=null)
for(final String s : nodeValue.split(" ")){
fop.write(s.getBytes());
fop.write(System.getProperty("line.separator").getBytes());
}
}
fop.flush();
fop.close();
}
Source: Stackoverflow.com