1

Hello my question is how to filter text file by column, I mean that I want to cut file from range between rows. My files are with this kind of data:

284015160747447;8935901990807474474;
284015160747448;8935901990807474482;
284015160747449;8935901990807474490;
284015160747450;8935901990807474508;
284015160747451;8935901990807474516;
284015160747452;8935901990807474524;
284015160747453;8935901990807474532;
284015160747454;8935901990807474540;
284015160747455;8935901990807474557;
284015160747456;8935901990807474565;
284015160747457;8935901990807474573;
284015160747458;8935901990807474581;
284015160747459;8935901990807474599;
284015160747460;8935901990807474607;
284015160747461;8935901990807474615;
284015160747462;8935901990807474623;
284015160747463;8935901990807474631;

I want the user to be able to filter what to be written from the original file to the newly created: Example from 8935901990807474490 to 8935901990807474532, and for this number the start row and the last row to be in this range.

public class Test_read_file {
    public static List<String> readFile() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader("C:\\\\\\\\Users\\\\\\\\Admin\\\\\\\\Desktop\\\\\\\\Work Files\\\\\\\\314-WO0000001133814\\\\\\\\Cards\\\\\\\\MBD10760_182.out"))) {
            List<String> listOfData = new ArrayList<>();
            String d;
            while ((d = br.readLine()) != null) {
                listOfData.add(d);
            }
            return listOfData;
        }
    }

    public static void writeFile(List<String> listOfData) throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\\\\\\\Users\\\\\\\\Admin\\\\\\\\Desktop\\\\\\\\Work Files\\\\\\\\314-WO0000001133814\\\\\\\\Cards\\\\\\\\MBD10760_187.out"))) {
            for (String str : listOfData) {
                bw.write(str);
                bw.newLine();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        List<String> data = readFile();
        writeFile(data);
    }
}
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Liverpool
  • 265
  • 7
  • 21
  • 1
    Can you elaborate a bit more what the columns mean? Is the example file showing a row per line? Or is it just a long string? – jbx Jan 27 '19 at 12:26
  • yeah it show the text file,it contains two columns,first:284015160747447 and second 8935901990807474474 separate by ;.I successfully read and write file but i want to filter the text file by the second column and filters all data between some range for example from 8935901990807474490 to 8935901990807474532 to write in the new file all rows between the two records – Liverpool Jan 27 '19 at 12:46
  • So each line contains 4 numbers? Also suppose the range is from `284015160747447` to `8935901990807474482`, does it mean you want to write the following numbers? - *284015160747447;8935901990807474474; 284015160747448;8935901990807474482;* – Nicholas K Jan 27 '19 at 13:08
  • this is one line:284015160747447;8935901990807474474.I wont to filter from the second column 8935901990807474474 and to save whole records which are between given range – Liverpool Jan 27 '19 at 13:16
  • So you want only numbers that appear in the second column to be written to the file given a range? Provide a sample output as well. – Nicholas K Jan 27 '19 at 13:20
  • Yes i want only the numbers between range – Liverpool Jan 27 '19 at 13:28

1 Answers1

1

Make the following changes to your code :

  1. Use a Scanner to accept the values to be filtered by :

    public static void main(String[] args) throws IOException {
        List<String> data = readFile();         
        Scanner sc = new Scanner(System.in);        
        System.out.println("Enter range");      
        long num1 = sc.nextLong();      
        long num2 = sc.nextLong();      
        writeFile(data, num1, num2);
        sc.close();     
    }
    
  2. Since you want only columns to be written by the 2nd column split it using ; and then add only the 1st index to the list. This ensures that only numbers of the second column are being added to the list.

    public static List<String> readFile() throws IOException {      
        try (BufferedReader br = new BufferedReader(new FileReader(PATH))) {
            List<String> listOfData = new ArrayList<>();            
            String d;
            while ((d = br.readLine()) != null) {
                String[] split = d.split(";");  // <-- change made here
                listOfData.add(split[1]);           
            }           
            return listOfData;      
         }  
    }
    
  3. Use the range passed in and it will write only number that fall between this range.

    public static void writeFile(List<String> listOfData, long num1, long num2) 
       throws IOException {
       try (BufferedWriter bw = new BufferedWriter(new FileWriter(OUT))) {          
            for (String str : listOfData) {
                // change made here
                if (Long.valueOf(str) >= num1 && Long.valueOf(str) <= num2) {
                    bw.write(str);
                    bw.newLine();
                }           
            }       
        }   
    }
    
Nicholas K
  • 15,148
  • 7
  • 31
  • 57