This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to append text to an existing file in Java
I ave a file already made in C:\myfile.txt and it has some data in it.. Now when I try to use File files = new File("C:\myfile.txt"); , It overwrites th orignal data and inserts the new data in it.. Is there a mechanism to aviod overwriting of old data?...
You could use append mode in one of the file writer classes:
FileWriter writer = new FileWriter("myfile.txt", true);
use the method new FileOutputStream(File,true) to append to an existing file.
Provide true in the constructor to append the file as told above
For more detailed control, use RandomAccessFile
Related
This question already has answers here:
Inserting text into an existing file via Java
(8 answers)
Closed 6 years ago.
In my project, we are writing a file using DataOutputStream. We are writing different data types like short, byte, int and long and we are using respective methods in DataOutputStream like writeShort(), writeByte() etc.
Now, I want to edit one record in this file at a particular offset. I know the offset from which that record starts but I am not sure what is the right approach of writing to the file because only method in DataOutputStream supporting offset is the one which takes byte[].
I want to write the whole record which is a combination of different data types as mentioned above.
Can someone please tell me what is the correct approach for this?
In your case, you should use RandomAccessFile in order to read and/or write some content in a file at a given location thanks to its method seek(long pos).
For example:
try (RandomAccessFile raf = new RandomAccessFile(filePath, "rw")) {
raf.seek(offset);
// do something here
}
NB: The methods writeShort(), writeByte() etc. and their read counterparts are directly available from the class RandomAccessFile so using it alone is enough.
This question already has answers here:
Read a file line by line in reverse order
(10 answers)
Closed 9 years ago.
I'm trying to implement a log structure file system as an operating system assignment. In it most recent data is placed at end of file. That's why I want to read text file "line-by-line" in reverse order. Is it possible?
Check out ReverseLineInputStream:
https://code.google.com/p/lt2-sander-marco/source/browse/src/leertaak2/ReverseLineInputStream.java?spec=svn15&r=15
It refers to the SO question posted at How to read file from end to start (in reverse order) in Java?
in = new BufferedReader (new InputStreamReader (new ReverseLineInputStream(file)));
while(true) {
String line = in.readLine();
if (line == null) {
break;
}
System.out.println("X:" + line);
}
(Thanks, #Mark O'Donohue)
If its line by line - you could pass all the lines into an arraylist and then read it backgrounds using a reverse for loop such as for(int i = list.size()-1;i>=0;i--)
Yes, but it requires reading through the entire file first. If the file is long, what you can do is split the file into several files, read each file into memory and write the data out to a new file from last line to first line of the old file. When the operation is done, you can delete all the temporary files you created.
How can i prevent prinwriter in java from overwriting what's inside of that particular file?
Ex. I have a student.txt file. I already have few names there. After running and modifying this How do I create a file and write to it in Java? whats inside of that file will be overwritten. I just want to add it to the new line.
Also, how can i possibly perform search?
PrintWriter out = new PrintWriter(new FileWriter("student.txt", true));
The true is the append parameter - which indicates whether the FileWriter should append to the file. If it was false it would overwrite existing data in the file.
What do you mean by "how can i possibly perform search"?
This question already has answers here:
Print java output to a file
(5 answers)
Closed 9 years ago.
My question is related to this one. However, I am looking for a way to append the text file over several runs. is there a way to write console output to a text file without erasing the old runs information? I am working on 30+ classes and it would be tedious to change System.out.println statements so I prefer sticking with the System.setOut solution.
I have the following code based on #Mac answer
PrintStream out = new PrintStream(new FileOutputStream("aa.txt"),true);
System.setOut(out);
but the file aa.txtdoes not append the results, am I missing something here?
When you create a FileWriter or FileOutputStream for your file, pass true as a second argument to the constructor of the FileWriter or FileOutputStream.
FileWriter(File file, boolean append)
FileWriter and FileOutputStream provide a constructor with an append flag. Just modify the referenced code accordingly
You should use:
PrintStream out = new PrintStream(new FileOutputStream("output.txt"),true);
System.setOut(out);
This question already has answers here:
Java: Find .txt files in specified folder
(8 answers)
Closed 8 years ago.
the title is pretty self explanatory. I need to be able to read the file names of all the .txt files in a specific folder and add them to an array. can this be done with the scanner class?
No. A Scanner reads data from files, streams, Strings or anything that implements the Readable interface. This has nothing to do with accessing the filesystem structure. You need:
http://docs.oracle.com/javase/tutorial/essential/io/dirs.html
no, you can't use scanner for that, scanner only helps you parse a string or a file's content, for what you want to achieve, have a look at this
Also, have a look at Scanner's documentation
No, the Scanner class is only used for reading data in file, not read files in a folder. To read the data from a file you have specify the name file in constructor of Scanner while creating the object.