FAQ Database Discussion Community


BufferedWriter, FileWriter, Output Stream…What's the most modern way to do these things?

java,file,logging,streamwriter,bufferedwriter
I'm trying to programically create a new a file in java, and write a string array to that file. If the file already exists, I just want to append another string array (This is like a log file). Whats the most 'modern' way to do something like this? Is FileWriter...

Timeout-based BufferedWriter flush

java,java-io,bufferedwriter
I'm using BufferedWriter with the default size of 8192 characters to write lines to a local file. The lines are read from socket inputstream using BufferedReader readLine method, blocking I/O. Average line length is 50 characters. It all works well and fast enough (over 1 mln lines per second) however...

BufferedWriter closed throws exception

java,android,exception,bufferedwriter
I have made a method to write a log in an external storage of Android. public int writeLog(String clase, String metodo, Object object){ try{ File folder = new File(Environment.getExternalStorageDirectory(), Constants.ROUTE_FILES_TEXT); if (!folder.exists()) { folder.mkdir(); } FileWriter fw = new FileWriter(folder.toString() + "/log.txt", true); BufferedWriter out = new BufferedWriter(fw); out.write("Hora: "...

Android File to String

java,android,inputstream,android-file,bufferedwriter
I want to read out a file in Android and get the content as a string. Then I want to send it to a server. But for testing I just create a file on the device and put the content into it: InputStream stream = getContentResolver().openInputStream(fileUri); BufferedReader reader = new...

Save a Jtable to a file I created with a boolean column

java,swing,jtable,boolean,bufferedwriter
I'm trying to save a jtable on exit but I can't because I have a boolean as one of my columns. Can anyone explain to me how to get rid of java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String? this is my code to save on Exit. private void btnBckMouseClicked(java.awt.event.MouseEvent evt)...

BufferedWriter can't desire change data

java,arrays,file,bufferedreader,bufferedwriter
i hope you can help me in this, I am reading a file with multiple lines. I use BufferedReader to separate the datas. If data 1 does not match with new data 1 input, it will use BufferedWriter to replace the new data 1 input into old data 1. With...

Buffered Writer and Reader Proper order/use

java,logic,bufferedreader,bufferedwriter
So I'm not sure where my mistake is in the order/use of BufferedWriter and BufferedReader. See for yourself. My program works all except for these methods. Here is where I declared stuff, pretty sure error is not here. fw = new FileWriter(file, true); bw = new BufferedWriter(fw); fr = new...

Java - printing ArrayList into a text file

java,file,arraylist,io,bufferedwriter
I'm trying to print my ArrayList into my text file, line by line. for (int s = 0; s < list.size(); s++) { wr.write(list.get(s)); } or for (String text : list) { wr.write(text); } How am I doing that? I think the problem is "BufferedWriter" writing the file over and...

Writing to file in java using BufferedWriter

java,filewriter,random-sample,bufferedwriter,file-writing
I'm trying to take some random samples from a file and do some operations and then write them to another file. But I'm having trouble writing to a file in the grepLine method which can be found below. Inside the while loop of the grepLine function, I get a blank...

File reading/writing proper order

java,file,bufferedreader,bufferedwriter
So I'm working on this program that needs to create a file, writes into that file, and then read from that file. However, the file is not being written into but is still being created. First, Here are my declarations from the constructor in my file doing stuff class (called...

Exception in thread “AWT-EventQueue-0” java.lang.StringIndexOutOfBoundsException: String index out of range: 8

java,swing,substring,jcombobox,bufferedwriter
I have several jComboBoxes and jTextFields on my jPanel. Selected and typed values saving in text file. PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter... outfile.write(jcb1.getSelectedItem().toString().substring(1, 8)); outfile.write(jTextField1.getText()); But when I allow jComboBoxes to be empty, I cannot save empty value of jcb1 and getting this error: Exception in thread "AWT-EventQueue-0"...

Java BufferedWriter and OutputStream .close() method

java,outputstream,bufferedwriter
I'm new in java and there is a question about BufferedWriter and OutputStream closing. I have some logic, where it is inconvenient to use try-with-resources: public static void writeFile(String fileName, String encoding, String payload) { BufferedWriter writer = null; OutputStream stream = null; try { boolean needGzip = payload.getBytes(encoding).length >...

Java read from one file and write into another file using methods

java,file-io,bufferedreader,bufferedwriter
I am learning Java and working on File IO and right now stuck in reading text from One file and write in another file. I am using two different methods first one for reading and displaying text in console from file #1 and using another method to write in file#2....

How to check if the next line is empty with a BufferedWriter

java,bufferedwriter
I'm using a buffered writer to write words in a text file, which works. But the writer starts at a new line every time, so you get all the words underneath each other with output.nextLine(); Can I check if a line is empty with an if statement so it wont...

Confusion about String's replaceAll method

java,parsing,bufferedreader,bufferedwriter,replaceall
I'm attempting the will essentially take an input file and write out an output file that has each word and punctuation of the input on a separate line. Example input: System.out.println("hey there"); Example output: System.out.println ( "hey there" ) ; here is my code: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException;...

Buffered Writer unable to process from array [closed]

java,arrays,ascii,filewriter,bufferedwriter
I am using a bufferedwriter to write to a file from an array: int[] scores = new int[5]; for (int i = 0; i < 5; i++) { scores[i] = 2; //Array of twos } try { BufferedWriter output = new BufferedWriter(new FileWriter(new File("Scores.txt"))); output.flush(); for (int i = 0;...

Java - open txt file and clear all multiple spaces

java,bufferedreader,bufferedwriter
I have a txt file and what I am trying to do is open it and delete all multiple spaces so they become only one. I use: br = new BufferedReader(new FileReader("C:\\Users\\Chris\\Desktop\\file_two.txt")); bw = new BufferedWriter(new FileWriter("C:\\Users\\Chris\\Desktop\\file_two.txt")); while ((current_line = br.readLine()) != null) { //System.out.println("Here."); current_line = current_line.replaceAll("\\s+", " ");...

Buffered writer in a loop is creating empty lines in txt file

java,bufferedwriter
Link to entire code: http://pastebin.com/Y0FA7zuG My code: public void dadUpdateFunction(ArrayList<JTextArea> texts) { try{ //Specify the file name and path here File file =new File("C:\\Users\\Karan\\Documents\\dadTXT.txt"); /* This logic is to create the file if the * file is not already present */ if(!file.exists()){ file.createNewFile(); } //Here true is to append the...

Java: BufferedReader Keeps Writing Values 128-159 as 63 When Converting to Char

java,bufferedreader,filereader,filewriter,bufferedwriter
I am trying to write a hex editor. I'm trying to store values by writing a char to a text file. For some reason every decimal number 128-159 is being written or read (not sure which) as 63. I took measures to isolate the problem. Here's an example of it...

Why it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters?

java,file,filewriter,bufferedwriter
My question is, whether it is direct use, as shown below; class Test{ public static void main(String args[]){ try{ FileWriter fo =new FileWriter("somex.txt"); int arr[] = {9}; for(int i =0; i <arr.length; i++){ fo .write(arr[i]); // this is very costly. } fo.write(b); fo.close(); System.out.println("...."); }catch(Exception e){system.out.println(e);} } } OR coding...

Java - Writing and Reading ArrayList to and from .txt file - BufferedWriter

java,bufferedreader,filereader,filewriter,bufferedwriter
Some Background: I'm making an RPG text based game for class, and I'm using an ArrayList for the inventory, since of course, it will constantly be changing whenever another item is added. I want to be able to save this ArrayList into a txt file and I want to be...

Why I am unable to get output while using BufferedWriter?

java,io,bufferedwriter
I am unable to get the output while using BufferedWriter. From my point of view it will show d aei durga [where i posted why no output particularly] import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.FileReader; public class BufferedWriterDemo { public static void main(String s[])throws Exception { //char...

BufferedWriter is not writing a new line in a file for a String having “\n” added to it

java,string,swing,newline,bufferedwriter
I was trying some stuff in Swing (Java), but getting very strange results. I am getting a String from JTextArea.getText() method and adding "\n" to it. This resultant string I am writing into a file, using BufferedWriter which is chaining through FileOutputStream to a file. But the new line character...

How to get the code display proper rows and columns in .csv format

java,csv,io,bufferedwriter,binaries
I have the following code. The problem is that its displaying back to back in columns. Here is a picture from my excel file: I want it to start over from next row once the record for single execution is filled from column to column. Here is my code: public...