java file input and output handling

ang3lus
ang3lus used Ask the Experts™
on
Hi
can someone help to deal with file
i have file that has format:
E008,Michelle,1980, manager, 17000
E008,George,1970, manager, 10600
E008,Jonathan,1940, manager, 25000

i don't have any problem with reading or writing to file

my question is:
there is a quick way to update specific element in file like salary
How i can while i am reading from file change element from specific line like salary
for example
E008,Michelle,1980, manager, 17000
E008,Michelle,1980, manager, 0

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Awarded 2011
Awarded 2011

Commented:
You cannot update one line or one item in the file,
you need to read the whole file and re-write it to a new
plcae and while doing it you can parse the contents get to the element you need and replace it.
Awarded 2011
Awarded 2011
Commented:

Unfortunately in general siuationtion you cannot modify a single item in the text file without
rewriting it. This is an example of the program which will do it in your case
for particular item

import java.io.*;
import java.util.StringTokenizer;

public class ModifyingFile {
    public ModifyingFile(){
        try{
            DataInputStream in = new DataInputStream(new FileInputStream("inputFile.txt"));
            PrintStream psout = new PrintStream(new FileOutputStream("outputFile.txt"));
            String buff = null;
            while((buff=in.readLine()) != null){
                if(buff.trim().length() == 0)continue;
                StringTokenizer t = new StringTokenizer(buff," ,");
                 String code = t.nextToken();
                String name = t.nextToken();
                String year = t.nextToken();
                String position = t.nextToken();
                String salary = t.nextToken();
                if (name.equals("Michelle") && position.equals("manager"))salary = "0"; 
                psout.println(code + "," + name + "," + year + ", " + position + ", " + salary );
                 
            }

            in.close();
            psout.close();
            File f0 = new File("inputFile.txt");
            File f1 = new File("outputFile.txt");
            f0.delete();
            f1.renameTo(f0);

        } catch(Exception ex){
            System.out.println(ex.toString());
            ex.printStackTrace();
        }

    }
 public static void main(String [] args){
   new ModifyingFile();
 }

}

Open in new window


input:
E008,Michelle,1980, manager, 17000
E008,George,1970, manager, 10600
E008,Jonathan,1940, manager, 25000

Open in new window


output of the above program run:
E008,Michelle,1980, manager, 0
E008,George,1970, manager, 10600
E008,Jonathan,1940, manager, 25000

Open in new window

Author

Commented:
thanks
Awarded 2011
Awarded 2011

Commented:
Just a word of caution - I'm sure you know but just in case: when running and especially debugging this sort of programs
in real life certainly make sure that you have copies of all your files - there's nothing easier to do thhan to lose
your the whole file with one wrong operator.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial