considering the following function
private static void GetText(String nodeValue) throws IOException {
if(!file3.exists()) {
file3.createNewFile();
You could print through a PrintStream.
PrintStream ps = new PrintStream(fop);
ps.println(nodeValue);
ps.close();
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();
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();
}
To write text (rather than raw bytes) to a file you should consider using FileWriter. You should also wrap it in a BufferedWriter which will then give you the newLine method.
To write each word on a new line, use String.split to break your text into an array of words.
So here's a simple test of your requirement:
public static void main(String[] args) throws Exception {
String nodeValue = "i am mostafa";
// you want to output to file
// BufferedWriter writer = new BufferedWriter(new FileWriter(file3, true));
// but let's print to console while debugging
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String[] words = nodeValue.split(" ");
for (String word: words) {
writer.write(word);
writer.newLine();
}
writer.close();
}
The output is:
i
am
mostafa