title: How to open and read a file and write specific content to a new file?
body: <p>First week learning Java. Apologies for my broken code below. Trying to open a file read it and choose needed contents to write in another file. My file looks like this:</p>
  
  <pre><code>DESCRIPTION:
  TITILE:
  TYPE: image
  SOURCE: Library
  FORMAT: jpeg 
  IDENTIFIER: 120034 
  LATITUDE: 40.109580 
  LONGITUDE: -88.228378 
  
  DESCRIPTION: 
  TITLE: GSLIS 
  SUBJECT: 
  TYPE: image 
  SOURCE: Library 
  FORMAT: jpeg 
  IDENTIFIER: 120155 
  LATITUDE: 40.107779 
  LONGITUDE:-88.231621
  </code></pre>
  
  <p>I just wrote two pieces of code, one for open and read, one for match the patterns:</p>
  
  <pre><code>package Functions;
  
  import java.io.IOException;
  import java.io.FileReader;
  import java.io.BufferedReader;
  
  public class readFileLocal {
      private static final String[] String = null;
      private String path;
  
      public readFileLocal(String file_path){
          path = file_path;
      }
  
      public String[] openFile() throws IOException{  
          FileReader freader = new FileReader (path);
          BufferedReader textReader = new BufferedReader (freader);
  
          int numberOfLines = readLines();   
          String[] textData = new String[numberOfLines]; 
  
          int i;  
          for (i=0; i&lt;numberOfLines; i++){
  
              String newLine=new String();
              newLine=null;
              textData[i] = textReader.readLine(); 
              String a = textData.toString();
              while ((textData[i])!=null){
                  if (a.startsWith("Identifier: ") || a.startsWith("Latitude: ")||a.startsWith("Longitude:")){
                      newLine.append(a);
                  }
  
                  boolean filled1 =Boolean.valueOf(String newLine[0]);
                  boolean filled2 =Boolean.valueOf(String newLine[1]);
                  boolean filled3 =Boolean.valueOf(String newLine[2]);
  
                  if(filled1, filled2, filled3) {
                         System.out.println(newLine[0]+'|'+newLine[1]+"|"+newLine[2]+"\n");
                  }
  
                  }
          }
      textReader.close();     
  }
  
  
      int readLines() throws IOException{
          FileReader file_to_read = new FileReader(path);
          BufferedReader lines = new BufferedReader (file_to_read);
  
          String aLine=lines.readLine();
          int numberOfLines = 0;
          while(aLine != null) { 
              numberOfLines ++; 
          }  
  
          lines.close();
          return numberOfLines;
      }   
  </code></pre>
  
  <p>}</p>
  
  <p>I also figured how to search a string in the way I wanted with people's help, as shown below:</p>
  
  <pre><code>import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  public class ReadLatLong
  {
  
      public ReadLatLong(){
  
         String line = "IDENTIFIER: 115956 LATITUDE: 40.104730 LONGITUDE: -88.228798 DATE RIGHTS IDENTIFIER: 115956 LATITUDE: 40.104730 LONGITUDE: -88.228798 DATE RIGHTS"; 
          String pattern = "IDENTIFIER:\\s(\\d*)\\sLATITUDE:\\s(\\d*\\.?\\d*)\\sLONGITUDE:\\s(.*?)\\s";
  
         Pattern r = Pattern.compile(pattern);
         Matcher m = r.matcher(line);
  
         while (m.find()) {
             System.out.println("Image: " + m.group(1)+"|"+m.group(2)+"|"+m.group(3));
         } 
     }
  }
  </code></pre>
  
  <p>Now I wonder how to search the whole file to grab all identifier, latitude, longitude, and put them all out like this:
  <code>120034 | 40.109580 | -88.228378 \n</code>
  <code>120155 | 40.107779 | -88.231621 \n</code></p>
  
  <p>This is my main method but I don't know how to write it in a new file either.</p>
  
  <pre><code>package readText;
  
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.PrintWriter;
  
  public class FileData {
  
      public static void main(String[] args) throws Exception {
  
          String file_path1 = "F:\\CampusImages.txt";
          String file_path2 = "F:\\CampusImageIDs.txt";
  
          try{
              ReadFileLocal file = new ReadFileLocal(file_path1);
              WriteFile writeout = new WriteFile( file_path2); //Don't know how to write it in new file.
              PrintWriter writer = new PrintWriter(new FileWriter(new File(file_path2)));
              String[] arylines = file.openFile();
              writeout.WriteToFile(String.valueOf(arylines));
  
              int i;
              for (i=0; i&lt;arylines.length; i++){
                  System.out.println(arylines[i]);
              }   
         writer.close();  
          }
  
          catch(IOException e) { 
              System.out.println(e.getMessage()); 
          }
      }
  }   
  </code></pre>
  
  <p>Thanks in advance.</p>