Reading files into a program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fazana
    New Member
    • Oct 2006
    • 8

    #1

    Reading files into a program

    I need help on how to read files into a program. For example, there are three lines of code into a text file. I have read all the lines but have to discard the first two lines.

    Can anyone tell me that how can i do that?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Fazana
    I need help on how to read files into a program. For example, there are three lines of code into a text file. I have read all the lines but have to discard the first two lines.

    Can anyone tell me that how can i do that?
    Code:
    import java.io.*;
    public class ReadFile {
            public static void main(String args[]) {
    	String append="";
    	try {
    	    FileReader fr;
    	    BufferedReader br;
    	    fr = new FileReader("C:/test.txt");
    	    br = new BufferedReader(fr);
    	    br.readLine(); br.readLine();//skip the first 2 lines
                        while((append = br.readLine()) != null) { 		 
                              System.out.println(append);
    	    }
       	}
    	catch(FileNotFoundException fN) {
      	     fN.printStackTrace();
    	}
    	catch(IOException e) {
    	         System.out.println(e);
    	}
            }
    }
    Next time try to write some code yourself first

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by alasoya
      Hi!! I have did some code but the substring don't seem to be working.

      <%
      // Upload the File

      String path=request.ge tParameter("fil ename");
      String newPath="";
      int count=0;

      if(path!=null)
      {
      ArrayList arr=new ArrayList();
      StringTokenizer st=new StringTokenizer (path,"\\");
      while(st.hasMor eTokens())
      {
      arr.add(count,s t.nextToken());
      count++;
      }

      // create ur own path

      newPath="E:/TEMP/upload/"+arr.get(c ount-1);
      int c;
      FileInputStream fis=new FileInputStream (path);
      FileOutputStrea m fos=new FileOutputStrea m(newPath);
      while((c=fis.re ad())!=-1)
      {
      fos.write((byte )c);
      }
      out.println("Up load successfully");

      }

      // Read the File


      try {
      File inFile = new File(newPath);
      BufferedReader br = new BufferedReader( new InputStreamRead er(
      new FileInputStream (inFile)));
      String DataLine = "";
      do{
      DataLine = br.readLine();
      out.println("<b r>"+DataLine) ;
      //out.println("<b r> Year: " + DataLine.subStr ing(0,4));
      out.println("<b r> Level: " + DataLine.substr ing(4,6));
      out.println("<b r> Class: " + DataLine.substr ing(6,14));
      out.println("<b r> SUID: " + DataLine.substr ing(14,19));
      out.println("<b r> School Type: " + DataLine.substr ing(19,20));
      out.println("<b r> Semester: " + DataLine.substr ing(20,21));

      }while(DataLine !=null);

      br.close();

      } catch (FileNotFoundEx ception ex) {
      out.println("Er ror File not found");
      } catch (IOException ex) {
      out.println("Er ror IO Exception ");
      }
      %>

      so can anyone tell me what is the error?
      If you have a problem, you should start your own thread

      Comment

      Working...