简体   繁体   中英

How can we concat a String at the end of each line in java?

I have some unique ID which have to be appened to the end of each line. I don't want to create another file, I want to do this in the same file which is used to read line by line and based on that line unique ID is generated.
I using java for this operation and here is my code,

String filename="location.txt";
    Path pathToFile = Paths.get(filename);
    FileWriter fstream = null;
    BufferedWriter bw=null;
    PrintWriter pw=null;
    try {
        fstream = new FileWriter(filename, true);
        bw=new BufferedWriter(fstream);
        pw=new PrintWriter(bw);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) { 
        // read the first line from the text file 
        String line = br.readLine(); 
        // loop until all lines are read 
        while (line!=null) { 
            // use string.split to load a string array with the values from 
            // each line of 
            // the file, using a " " as the delimiter
            String[] attributes = line.split(" "); 

            System.out.print("Sensor["+attributes[0]+"] : ");
            double x=Double.parseDouble(attributes[1]);
            double y=Double.parseDouble(attributes[2]);
            GeoHash geoCode=GeoHash.withCharacterPrecision(x, y, 10);
            System.out.print(geoCode+"\n");
            pw.println(line+" "+geoCode);

            line = br.readLine(); 
        } 
        br.close();
        pw.close();
        bw.close();
        fstream.close();
    } catch (IOException ioe) { 
            ioe.printStackTrace(); 
    }
    System.out.println("Generated!!");
} 

Currently by running this code i will get the appended line as new line at the end of my file as below: OUTPUT:

1 21.5 23
2 24.5 20
3 19.5 19
4 22.5 15
5 24.5 121 21.5 23 seb673undf
2 24.5 20 skq5rh5dcg
3 19.5 19 s7mwbmg7sp
4 22.5 15 sk48j248j2
5 24.5 12 sk2e3h44u7

Is there any way to solve this? or any other simple method to do it?

Instead of writing each line to a file, append the data to string builder for each while loop after completion your file reading you just clear the data in the file after that just write the string builder data to that file .

    package application;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.text.ParseException;

    public class T {
        public static void main(String[] args) throws ParseException {
            String filename="location.txt";
            Path pathToFile = Paths.get(filename);
            FileWriter fstream = null;
            BufferedWriter bw=null;
            PrintWriter pw=null;
            try {
                fstream = new FileWriter(filename, true);
                bw=new BufferedWriter(fstream);
                pw=new PrintWriter(bw);
            } catch (IOException e) {
                e.printStackTrace();
            }

            StringBuilder sb = new StringBuilder();
            try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) { 
                String line = br.readLine(); 
                while (line!=null) { 
                    String[] attributes = line.split(" "); 
                    System.out.print("Sensor["+attributes[0]+"] : ");
                    double x=Double.parseDouble(attributes[1]);
                    double y=Double.parseDouble(attributes[2]);
                    GeoHash geoCode=GeoHash.withCharacterPrecision(x, y, 10);
                    System.out.print(geoCode+"\n");
                    pw.println(line+" "+geoCode);
                    sb.append(line+" "+geoCode);
                    line = br.readLine(); 
                } 
                PrintWriter writer = new PrintWriter(filename);
                writer.print("");
                writer.close();
                br.close();
                pw.close();
                bw.close();
                fstream.close();
            } catch (IOException ioe) { 
                    ioe.printStackTrace(); 
            }
            System.out.println("Generated!!");
        } 

    }

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