filewriter

Writing multiline JTextArea content into file

旧时模样 提交于 2019-11-26 17:23:00
问题 I have to write the content of textarea into a file with line breaks. I got the output like, it is written as one string in the file. public void actionPerformed(ActionEvent ev) { text.setVisible(true); String str= text.getText(); System.out.println(str); try { BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt")); fileOut.write(str); fileOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } Example Output should be: I am King. but it is showing: IamKing.

PrintWriter vs FileWriter in Java

心不动则不痛 提交于 2019-11-26 11:58:41
问题 Are PrintWriter and FileWriter in Java the same and no matter which one to use? So far I have used both because their results are the same. Is there some special cases where it makes sense to prefer one over the other? public static void main(String[] args) { File fpw = new File(\"printwriter.txt\"); File fwp = new File(\"filewriter.txt\"); try { PrintWriter pw = new PrintWriter(fpw); FileWriter fw = new FileWriter(fwp); pw.write(\"printwriter text\\r\\n\"); fw.write(\"filewriter text\\r\\n\"

Create whole path automatically when writing to a new file

余生颓废 提交于 2019-11-26 11:48:14
问题 I want to write a new file with the FileWriter. I use it like this: FileWriter newJsp = new FileWriter(\"C:\\\\user\\Desktop\\dir1\\dir2\\filename.txt\"); Now dir1 and dir2 currently don\'t exist. I want Java to create them automatically if they aren\'t already there. Actually Java should set up the whole file path if not already existing. How can I achieve this? 回答1: Something like: File file = new File("C:\\user\\Desktop\\dir1\\dir2\\filename.txt"); file.getParentFile().mkdirs(); FileWriter

Modify the content of a file using Java

五迷三道 提交于 2019-11-26 11:25:05
问题 I want to delete some content of file using java program as below. Is this the write method to replace in the same file or it should be copied to the another file. But its deleting the all content of the file. class FileReplace { ArrayList<String> lines = new ArrayList<String>(); String line = null; public void doIt() { try { File f1 = new File(\"d:/new folder/t1.htm\"); FileReader fr = new FileReader(f1); BufferedReader br = new BufferedReader(fr); while (line = br.readLine() != null) { if

Trouble with filewriter overwriting files instead of appending to the end

女生的网名这么多〃 提交于 2019-11-26 09:56:55
问题 OK, I\'m having some trouble writing multiple lines to a text file. the program runs, but it won\'t use new lines each time when I want it run 4 times, the text file should look like: a b c d instead, it looks like: d who knows how to fix this problem? all imports are correctly imported. source(it\'s been slightly edited, assume everything is properly defined): import java.io.*; public class Compiler { public static void main (String args[]) throws IOException { //there\'s lots of code here

Create a new line in Java&#39;s FileWriter

China☆狼群 提交于 2019-11-26 07:40:29
问题 I have coded the following FileWriter : try { FileWriter writer = new FileWriter(new File(\"file.txt\"), false); String sizeX = jTextField1.getText(); String sizeY = jTextField2.getText(); writer.write(sizeX); writer.write(sizeY); writer.flush(); writer.close(); } catch (IOException ex) {} Now I want to insert a new line, just like you would do it with \\n normally, but it doesn\'t seem to work. What can be done to solve this? Thank you. 回答1: If you want to get new line characters used in