Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: parse txt file

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default parse txt file

    Hi,

    i have a txt files i want to parse and when for example i see "test" i want to get a value test has in the txt file.For example the line is

    var text = 2;

    i want to see text but i want to assign 2 in a variable.new to java guys.thnx a lot
    here's the code reading a fileei think

    try
      {
      FileInputStream in = new FileInputStream("inputFile.txt");
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
     
      while((strLine = br.readLine())!= null)
      {
       System.out.println(strLine);
      }
     
      }catch(Exception e){
       System.out.println(e);
      }


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    Is that code right?

    class ReadWithScanner {
     
                public void main(String... aArgs) throws FileNotFoundException {
                    ReadWithScanner parser = new ReadWithScanner("C:\\marinos/PerfSim-CF-Config.txt");
                    parser.processLineByLine();
                    log("Done.");
                }
     
     
            public ReadWithScanner(String aFileName){
                fFile = new File(aFileName);
            }
     
             /** Template method that calls {@link #processLine(String)}.  */
            public final void processLineByLine() throws FileNotFoundException {
            //Note that FileReader is used, not File, since File is not Closeable
            Scanner scanner = new Scanner(new FileReader(fFile));
            try {
            //first use a Scanner to get each line
                while ( scanner.hasNextLine() ){
                    processLine( scanner.nextLine() );
                }
            }
            finally {
          //ensure the underlying stream is always closed
          //this only has any effect if the item passed to the Scanner
          //constructor implements Closeable (which it does in this case).
          scanner.close();
        }
      }
     
      /**
       Overridable method for processing lines in different ways.
     
       <P>This simple default implementation expects simple name-value pairs, separated by an
       '=' sign. Examples of valid input :
       <tt>height = 167cm</tt>
       <tt>mass =  65kg</tt>
       <tt>disposition =  "grumpy"</tt>
       <tt>this is the name = this is the value</tt>
      */
      protected void processLine(String aLine){
        //use a second Scanner to parse the content of each line
        Scanner scanner = new Scanner(aLine);
        scanner.useDelimiter("=");
        if ( scanner.hasNext() ){
          String name = scanner.next();
     
          log("Path = " + quote(name.trim()));
        }
        else {
          log("Empty or invalid line. Unable to process.");
        }
        //no need to call scanner.close(), since the source is a String
      }
     
      // PRIVATE
      private final File fFile;
     
      private  void log(Object aObject){
        System.out.println(String.valueOf(aObject));
      }
     
      private String quote(String aText){
        String QUOTE = "'";
        return QUOTE + aText + QUOTE;
      }
    }

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: parse txt file

    Can you show us an example input file please?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    Path = C://MARINOS
    var Access = 4
    var somthng = 6

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    the problem in code is that name has private access..i think the code before is doing the job but variable name has private access
    Last edited by maliv; November 17th, 2010 at 04:56 AM.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: parse txt file

    Path = C://MARINOS
    var Access = 4
    var somthng = 6

    Is that the contents of the input file?

    You say you want the value of test. So in the file, will test look like:

    var test = 4

    ??!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    for example i want to take C://MARINOS in a variable
    after the Path =

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: parse txt file

    You could do something like this:

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
     
    public class ParseFile {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		String pathVar = null;
     
    		try {
    			FileInputStream in = new FileInputStream("file.txt");
    			BufferedReader br = new BufferedReader(new InputStreamReader(in));
    			String strLine;
     
    			while ((strLine = br.readLine()) != null) {
    				//System.out.println(strLine);
    				if (strLine.contains("Path")) {
    					pathVar = strLine.replace("Path", "").trim();
    					pathVar = pathVar.replace("=", "").trim();
    				}
    			}
     
    			System.out.println(pathVar);
     
    		} catch (Exception e) {
    			System.out.println(e);
    		}
     
    	}
     
    }

    If the input file contains:

    Path = C://MARINOS


    Then C://MARINOS will be stored in pathVar
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #9
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    the problem is tha i have an event in button

    and i can't name public class i don't know why

  10. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    public static void main(String[] args) {
     
            String pathVar = null;
     
            try {
                FileInputStream in = new FileInputStream("file.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
     
                while ((strLine = br.readLine()) != null) {
                    //System.out.println(strLine);
                    if (strLine.contains("Path")) {
                        pathVar = strLine.replace("Path", "").trim();
                        pathVar = pathVar.replace("=", "").trim();
                    }
                }
     
                System.out.println(pathVar);
     
            } catch (Exception e) {
                System.out.println(e);
            }
     
     
            FolderName = (FolderNameTextfield.getText());
     
            //Create root directoey from textfield and sub-folders
            File a1 = new File("C://marinos/"+ FolderName +"");
            File a2 = new File("C://marinos/"+ FolderName +"/Configuration");
            File a3 = new File("C://marinos/"+ FolderName +"/Configuration/Assembly");
            File a4 = new File("C://marinos/"+ FolderName +"/Configuration/PIT");
            File a5 = new File("C://marinos/"+ FolderName +"/Configuration/SDB");
            File a6 = new File("C://marinos/"+ FolderName +"/Scripts");
            File a7 = new File("C://marinos/"+ FolderName +"/Scripts/Assembly");
            File a8 = new File("C://marinos/"+ FolderName +"/Scripts/PIT");
            a1.mkdir();
            a2.mkdir();
            a3.mkdir();
            a4.mkdir();
            a5.mkdir();
            a6.mkdir();
            a7.mkdir();
            a8.mkdir();
    ...
    }

  11. #11
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    sorry this is the code
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
            //scan PerfSim-CF-Config.txt file
          public class ParseFile {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            String pathVar = null;
     
            try {
                FileInputStream in = new FileInputStream("file.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
     
                while ((strLine = br.readLine()) != null) {
                    System.out.println(strLine);
                    if (strLine.contains("Path")) {
                        pathVar = strLine.replace("Path", "").trim();
                        pathVar = pathVar.replace("=", "").trim();
                    }
                }
     
                System.out.println(pathVar);
     
            } catch (Exception e) {
                System.out.println(e);
            }
     
     
            FolderName = (FolderNameTextfield.getText());
     
            //Create root directoey from textfield and sub-folders
            File a1 = new File("C://marinos/"+ FolderName +"");
            File a2 = new File("C://marinos/"+ FolderName +"/Configuration");
            File a3 = new File("C://marinos/"+ FolderName +"/Configuration/Assembly");
            File a4 = new File("C://marinos/"+ FolderName +"/Configuration/PIT");
            File a5 = new File("C://marinos/"+ FolderName +"/Configuration/SDB");
            File a6 = new File("C://marinos/"+ FolderName +"/Scripts");
            File a7 = new File("C://marinos/"+ FolderName +"/Scripts/Assembly");
            File a8 = new File("C://marinos/"+ FolderName +"/Scripts/PIT");
            a1.mkdir();
            a2.mkdir();
            a3.mkdir();
            a4.mkdir();
            a5.mkdir();
            a6.mkdir();
            a7.mkdir();
            a8.mkdir();
    ...
     
    }

  12. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: parse txt file

    Can you post your entire code.. Something I can compile?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  13. #13
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    it says
    The text that you have entered is too long (332244 characters). Please shorten it to 100000 characters long.so ican't

  14. #14
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    thnx for the help

  15. #15
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: parse txt file

    Quote Originally Posted by maliv View Post
    it says
    The text that you have entered is too long (332244 characters). Please shorten it to 100000 characters long.so ican't
    Oh OK. Are you able to add it to a zip file and attach it?
    I'm a bit confused as to what the problem is so i'd like to see the code.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  16. #16
    Junior Member
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: parse txt file

    U CAN REname it as solved.i did it.thnx a lot for your help!!!i read other code as well micxed the code and VOILA!!!

  17. #17
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: parse txt file

    Quote Originally Posted by maliv View Post
    U CAN REname it as solved.i did it.thnx a lot for your help!!!i read other code as well micxed the code and VOILA!!!
    OK cool. Glad I could help. I have marked this thread as solved
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  18. The Following User Says Thank You to JavaPF For This Useful Post:

    javapenguin (March 26th, 2011)

Similar Threads

  1. How to parse an object to and from XML using JAXB
    By Json in forum File Input/Output Tutorials
    Replies: 1
    Last Post: August 8th, 2013, 05:45 PM
  2. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  3. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  4. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  5. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM