简体   繁体   中英

edit a line in text file

so i have a text file that consist:

not voting/1/harold/18
not voting/2/isabel/24

this describes like not voting/number for vote/name/age . my goal is to edit not voting to voted but still keeping the other info ( number for vote/name/age ). the user will input the number for vote then if it exist, the not voting will automatically change to voted

here is my code:

            File original = new File("C:\\voters.txt");     
            File temporary = new File("C:\\tempvoters.txt");

            BufferedReader infile = new BufferedReader(new FileReader(original));
            PrintWriter outfile = new PrintWriter(new PrintWriter(temporary));

            numberforvote=JOptionPane.showInputDialog("Enter voters number: ");
            String line=null;

            while((line=infile.readLine())!=null){

                String [] info=line.split("/");
                if(info[1].matches(numberforvote)){
                    all="VOTED"+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
                    outfile.println(all);
                    outfile.flush();
                }
            }
        infile.close();
        outfile.close();

        original.delete();
        temporary.renameTo(original);

this works but the problem with my code is the second line ( not voting/2/isabel/24 ) will disappear/deleted. i want everything to be the same except for the not voting in the given/entered number for vote.

if(info[1].matches(numberforvote)){
     all="VOTED"+"/"+info[1]+"/"...;
     outfile.println(all);
     outfile.flush();
} else {
     outfile.println( line );
}

Copy to output if there's no match.

I should add that using a regex for a single string compare should be reduced to the simpler info[1].equals(numberforvote) . But calling numberforvote = numberforvote.trim(); could be useful.

Your output file gets entirely overwritten , so you have to write all lines, even those that you didn't intend to modify :

      if(info[1].matches(numberforvote)){
            all="VOTED"+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
            outfile.println(all);
       }
      else{
            outfile.println(line); // this will write the "unchanged" lines
       }

        outfile.flush();

Move it outside the if and only change the part you need to change. That way you can change whatever you want then rebuild the line.

if(info[1].matches(numberforvote)){
  into[0] = VOTED;
}

all=info[0]+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];

outfile.println(all);
outfile.flush();

or clean up that ugly line

 StringBuilder sb = new StringBuilder();
 for (String element : info){
    sb.append(element);
 }
 outfile.println(sb.toString());

Other Answers

You could just output the unchanged line like others have suggested

outfile.println(line);

but it's not as flexable if you want to make other changes later.

You should simplify the split and writing to:

String [] info=line.split("/",2);
if ( info.length == 2 ) {
   ...
   outfile.println("VOTED/"+info[1]);
} else {
  // input error
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM