Java save temporary data to file - java

I have this code:
for (Record record : Adatok) {
//System.out.println(record.toString2());
act_data=tmp.testtestclass(record);
System.out.println("*******");
System.out.println("Feldolgozás eredménye:");
System.out.println(data_restructure(act_data));
// String content = record.nev + ";" + record.address + "\n"+"asd";
File file = new File("resultset.csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data_restructure(act_data) + "\n");
bw.close();
}
My problem is that this loop running time is hours and can be interupting. So I do this filewrite/bufferedwrite in it.
So everytime he get data back than I want to write it to file.
But when I do this it is always write only 1 line to my file than nothing.
How can I improve it? I tried with firewriter, bufferedwriter but It kinda bugging.
I know its a dumb question but I cant figurit out how to solve it cause the basic examples does not works.

You can try to use FileWriter with TRUE parameter:
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true); //see here!
BufferedWriter bw = new BufferedWriter(fw);
By doing this, new Text will be appended to the file.

Put these lines outside (before) the loop. You are overwriting the file in each loop iteration.
File file = new File("resultset.csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
Also,
bw.close(); // outside (after) the loop

Related

How can I make PrintWriter NOT append to a file?

I have a server that writes to a logfile, but I do not want to append lines. I have set the flag to false, but still it seems to be appending. How can I make it REPLACE the first line everytime so my file contains one updated line everytime ?
fos = new FileOutputStream(new File(filename), false);
PrintWriter bw = new PrintWriter(new OutputStreamWriter(fos));
..
..
while(true){
line = getRandomLine();
bw.println(line);
bw.flush();
}
..
..
If I understand You correctly, you want this:
File file = new File(filename);
while (true) {
pw = new PrintWriter(file);
line = getRandomLine();
pw.println(line);
pw.flush();
pw.close();
}

How to create a java.io.Writer from a file without clean the file in java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java append to file
How to append data to a file?
I want to write a file in java without cleaning(deleting) older data
This is my try, but the current data will be cleaned on writing new data.
import java.io.*;
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "New content to write to file";
File file = new File("/mypath/filename.txt");
// if file doesnt exists, then create it
if (!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use constructor FileWriter(String filename, boolean append) that can instruct the file to be opened in append mode:
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
//^^^^ means append
Try
FileWriter fw = new FileWriter(file, true);
Notes: second param means append; no need for file.getAbsoluteFile(), just File is OK
open the file in append mode .
like
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename", true)));
FileWriter takes a boolean argument which specifies whether to overwrite or not.
Try this :
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
also visit :
http://docs.oracle.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29

Create a file using variables for the file name java

I'm trying to create a file and save the file as a name given as input and the iteration number. But this isn't writing anything to the source folder. I don't know where I could be going wrong.
private void write() throws IOException {
File file = new File(name + it_number + ".txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write(results);
it_number++;
writer.flush();
writer.close();
}
Not a fix, but a debug hint - to find where the file is going to be written:
File file = new File(name + it_number + ".txt");
System.out.println(file.getAbsolutePath());
Try to use BufferedWriter as below
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(results);
bw.close();

Write File without deleting current data [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java append to file
How to append data to a file?
I want to write a file in java without cleaning(deleting) older data
This is my try, but the current data will be cleaned on writing new data.
import java.io.*;
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "New content to write to file";
File file = new File("/mypath/filename.txt");
// if file doesnt exists, then create it
if (!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Use constructor FileWriter(String filename, boolean append) that can instruct the file to be opened in append mode:
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
//^^^^ means append
Try
FileWriter fw = new FileWriter(file, true);
Notes: second param means append; no need for file.getAbsoluteFile(), just File is OK
open the file in append mode .
like
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename", true)));
FileWriter takes a boolean argument which specifies whether to overwrite or not.
Try this :
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
also visit :
http://docs.oracle.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29

Writing to a temporary file in java

I want to write to a temporary file in an append mode. I see that the file is created but the data from the Stringbuffer is not getting written to it. Can somebody tell me why? Please find below the code I have written,
public static void writeToFile(String pFilename, StringBuffer sb)
throws IOException {
String property = "java.io.tmpdir";
String tempDir = System.getProperty(property);
File dir = new File(tempDir);
File filename = File.createTempFile(pFilename, ".tmp", dir);
FileWriter fileWriter = new FileWriter(filename.getName(), true);
System.out.println(filename.getName());
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(sb.toString());
bw.close();
}
This works:
public static void writeToFile(String pFilename, StringBuffer sb) throws IOException {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File tempFile = File.createTempFile(pFilename, ".tmp", tempDir);
FileWriter fileWriter = new FileWriter(tempFile, true);
System.out.println(tempFile.getAbsolutePath());
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(sb.toString());
bw.close();
}
Note the usage of FileWriter(File, boolean) and of System.out.println(tempFile.getAbsolutePath()).
FileWriter fileWriter = new FileWriter(filename.getName(), true);
should be
FileWriter fileWriter = new FileWriter(filename, true);
Instead of creating file in temp directory , create the file in your working directory and use objFile.deleteOnExit().It will also work the same as creating file in temp dir.
Try to call bw.flush() before closing the writer. Although I think that writer should call flush automatically before being closed...
FileWriter fileWriter = new FileWriter(filename.getName(), true);
should be
FileWriter fileWriter = new FileWriter(filename, true);
you can also use this
FileWriter fileWriter = new FileWriter(filename.getAbsolutePath+filename.getName(), true);
note
`filename.getName();`
returns the filename without the absolute path. So there might be the case that it is creating a file in the Present working directory and writing into it.

Categories