File management in Java

Solved
sagon -  
 Martin -
Hello,
how can we write in the middle of a file with Java

thank you in advance

4 answers

KX Posted messages 19031 Status Moderator 3 020
 
What do you mean by "the environment"?
What exactly do you want to do?
--
Trust does not exclude control.
0
arth Posted messages 84 Registration date   Status Contributor Last intervention   1 293
 
I think he wants to stream the file, a kind of sed, you know.
--

The wolf, solitary and mysterious.
0
KX Posted messages 19031 Status Moderator 3 020
 
I wasn't familiar with the term SEd, but if that's the case, we will need to load the text into a Collection, maybe an ArrayList<String>, perform the operations on it, and then overwrite the text.
In any case, I'm not aware of any streams in Java that allow for both reading (InputSream) and writing (OutputStream) at the same time.
--
Trust does not exclude control.
0
Martin
 
Hello,
I am slowly getting back into programming and I'm facing the same question about how to manage/append to a file without overwriting it every time.
I have created a small program that keeps track of my internship applications (three string fields like company_name, activity, and additional_information) and allows me to review them later.
To start off gently, I chose to write each entry in a text file in three lines (one for each field).
In my procedure for saving a new entry, I have the problem of appending to an existing file, and I am using an auxiliary array of type String[] to save what is already written in the file, because my other attempts to read and write at the same time have failed since the recreated file always overwrites the old one (new FileReader (same file name) the old one and its contents).

public void save_entry() {
String line1;
String line2;
String line3;
String[] aux = new String[100];
int i=0;
line1= textField1.getText();
line2=jTextField1.getText();
line3=jTextField2.getText();
try {
JFileChooser chooser = new JFileChooser(); // Dialog box to select the file to append to
chooser.showOpenDialog(new JFrame());
FileReader existing_file = new FileReader (chooser.getSelectedFile());
read_existing_file(existing_file, aux); // Reads the file to append to and saves each line in a String[]
BufferedWriter file = new BufferedWriter(new FileWriter(chooser.getSelectedFile())); // Creates the "same" file
while (aux[i]!=null) {
file.write(aux[i]); // Writes the lines saved in the String[]
file.newLine();
i++;
}
file.write(line1); // Writes the elements of the new entry
file.newLine();
file.write(line2);
file.newLine();
file.write(line3);
file.close();

}
catch (java.io.IOException ex) {
System.out.println("error" + ex);
}}

It works but I am not sure it's the best way to proceed.
Is there a trick to "break the unidirectional" nature of the stream?
Moreover, during reading, you can't create a file by typing a non-existent file name in the dialog box, can you?
Also, is it possible not to specify the length of the String[], it seems to me that it was possible in Pascal (that's how I did my databases)?
Don't worry too much about the syntax, especially the big try/catch block that encompasses everything!
Thank you in advance for your responses!
Martin
0
KX Posted messages 19031 Status Moderator 3 020
 
The problem with sagon was to add data in the middle of the file, while if I understood correctly, you could add your data at the end of the file without any issues with FileWriter(file,true);.

For your other question: the String[] is a bit heavy for manipulating your data, it would be better to go back to what I was saying and use a Collection.
0
Martin
 
Thank you for your response!
I'm gradually getting back into it, so it's true that a text file has the advantage of being easy to manage. I was thinking of modifying the code to create a record-type file (which should be more efficient). Is a collection equivalent to a record?
I will try your tip to write in the end! Thank you.
0
KX Posted messages 19031 Status Moderator 3 020
 
A Collection is "equivalent" to a container, like your String[]. For example, you can create an ArrayList<String> that allows you to add as many lines as you want without knowing in advance how many lines there will be, and access them directly like an array.
0
Martin
 
Alright, it's better that way when we have an indefinite number of entries!
I'll look into all that!
Thanks!
0