Results 1 to 12 of 12
  1. #1
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Thumbs down get string from a text file

    Is it possible to let java grab a string from a textfile?

    f.e.:
    config.txt
    editor: gedit
    name: Kruptein
    ...


    Is it possible that I can grab the string gedit if I want to know which editor is set as default?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Sure, just read through the text file. After reading each line, see if it contains editor: and extract the next token. One way to do this would be to use a Scanner object and use a while (scanner.hasNextLine()) to extract the nextLine() from the file. Then you could use a String method to see if the read line contains "name:", if so, split the line, with String#split(...) and extract your gedit string.

  3. #3
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Sorry but I only know java for say 2 days.

    I've found this:

    logdialtext = "Where is the configurationfile located? \n(whole the path)";
    String configpath = WelcomeMsg.showInputDialog(logdialtext);
    Scanner sc = new Scanner(configpath);
    sc.findInLine("editor:");
    if (sc.hasNext()) {
    WelcomeText.setText(sc.next());
    } else {
    WelcomeText.setText("Error!");
    }

    config.ssa
    editor: gedit
    filepath: /home/darragh/Documenten/D_MENU/files

    When I insert the path in the dialog,
    it just shows the path I insert into the WelcomeText; textbox

    What did I do wrong?

    I got this script from: java2s.com/Tutorial/Java/0180__File/Searchingforthespecifiedpatternwithinthenextlineof text.htm
    Last edited by Kruptein; 08-08-2009 at 02:30 PM. Reason: found a possible solution on my own

  4. #4
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    I think the problem is that it doesn't read the content of configpath, but only the name,

    How can I solve this?

    Kruptein

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    Hi,
    Use it like this and catch the "File Not Found Exception"

    Scanner sc = new Scanner(new File(configpath));
    Ramya:cool:

  6. #6
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Okay now I've the foll. code:

    Java Code:
    private void ConfigActionPerformed(java.awt.event.ActionEvent evt) {                                       
            try {
                logdialtext = "Where is the configurationfile located? \n(whole the path)";
                String configpath = WelcomeMsg.showInputDialog(logdialtext);
                Scanner sc = new Scanner(new File(configpath));
                sc.findInLine("editor:");
                if (sc.hasNext()) {
                    WelcomeText.setText(sc.next());
                } else {
                    WelcomeText.setText("Error!");
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(D_MENU.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    No if I give the path to the config file
    it only returns #*FileName=*config.ssa# in my textbox
    config.ssa
    Java Code:
    #*FileName=*config.ssa#
    editor: gedit
    filepath: /home/darragh/Documenten/D_MENU/files
    It still doesn't give me the string after editor... :(

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    Still u have not given the complete code.If u give it in "if" it will do only one statement parsing know? How can you get all?

    U should use while loop or for parsing the entire file know?



    Be specific with the requirement and give the full code with comments.Then only people can get what u want.
    Ramya:cool:

  8. #8
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    I can't be more specific, I want to get the string behind editor:

    Java Code:
    private void ConfigActionPerformed(java.awt.event.ActionEvent evt) {                                       
            try {
                logdialtext = "Where is the configurationfile located? \n(whole the path)";   //logdialtext is a dialogbox
                String configpath = WelcomeMsg.showInputDialog(logdialtext);
                Scanner sc = new Scanner(new File(configpath));
                if(sc.findInLine("editor:")) {
                    if (sc.hasNext()) {
                        WelcomeText.setText(sc.next()); //WelcomeText = TextBox.
                    } else {
                        WelcomeText.setText("Error!");
                    }
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(D_MENU.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    Like you can see in the code, I tried the if statement to see if it can find the word editor,
    but Netbeans give me the foll. error:
    Incompatible types, required: boolean
    (error about the line if(sc.findInLine("editor:")) )

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    One small correction.

    findinLine never returns boolean .So u can't use with if ..

    This method will set the pointer to read only if the text has "editor"

    Check it is working or not.

    One more doubt for me is print the configpath in S.O.P
    and send the textfile also for me.Let me have a look.

    private void ConfigActionPerformed(java.awt.event.ActionEvent evt) {
    try {
    logdialtext = "Where is the configurationfile located? \n(whole the path)";
    String configpath = WelcomeMsg.showInputDialog(logdialtext);
    Scanner sc = new Scanner(new File(configpath));
    sc.findInLine("editor:");
    if (sc.hasNext()) {
    WelcomeText.setText(sc.next());
    } else {
    WelcomeText.setText("Error!");
    }
    } catch (FileNotFoundException ex) {
    Logger.getLogger(D_MENU.class.getName()).log(Level .SEVERE, null, ex);
    }
    }
    Ramya:cool:

  10. #10
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Okay so I currently have:
    Java Code:
    private void ConfigActionPerformed(java.awt.event.ActionEvent evt) {                                       
            try {
                logdialtext = "Where is the configurationfile located? \n(whole the path)";
                //String configpath = WelcomeMsg.showInputDialog(logdialtext);
                String configpath = "/home/darragh/Documenten/D_MENU/files/config.ssa";
                Scanner sc = new Scanner(new File(configpath));
                sc.findInLine("editor:");
                if (sc.hasNext()) {
                    WelcomeText.setText(sc.next());
                } else {
                    WelcomeText.setText("Error!");
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(D_MENU.class.getName()).log(Level .SEVERE, null, ex);
            }
        }
    and config.ssa:
    Java Code:
    #*FileName=*config.ssa#
    editor: gedit
    filepath: /home/darragh/Documenten/D_MENU/files
    I'm trying to get the string after editor: (in this case gedit)

    Can you say how I should do this

    (Sorry but I didn't understand whole your answer)

  11. #11
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    As Fubarable said ,I have written a small code here.Please go thru whether it fits ur requirement and modify as per ur need.
    Currently this below code only retreives the editor details

    1.Iam having a file "Test.ssa and having contents as u said like this below

    editor: gedit
    name: Kruptein
    editor :fff
    name:Ramya
    editor:
    name:Ranjani


    2.I kept the above file in the same directory as of java for simplicity sake.
    See the code for retreival of editor information only.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    
    public class Test {
    
    	public static void main(String[] args) throws Exception
    	{
    
                Scanner sc = new Scanner(new File("Test.ssa"));
    			String arr[];
                     while (sc.hasNextLine())
    				 {
                        String input = sc.nextLine();
                        if(input.indexOf("editor") !=-1)
    					{
    						arr = input.split(":");
    						System.out.println(arr[1]);
    					}//if
    				}
    
    	}//main
    } //class
    Last edited by RamyaSivakanth; 08-14-2009 at 03:38 PM.
    Ramya:cool:

  12. #12
    Kruptein is offline Member
    Join Date
    Aug 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks! that solved my problem :),
    Sorry if I was to annoying ;p

Similar Threads

  1. how to store string in text file
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2009, 06:21 AM
  2. How to write a string middle of a text file?
    By loggen in forum New To Java
    Replies: 5
    Last Post: 12-19-2008, 08:48 AM
  3. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  4. Searching a string from a text file using Swing Buttons
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 2
    Last Post: 09-15-2008, 09:50 AM
  5. Searching a String from Text file using Swings .
    By pradeep1_mca@yahoo.com in forum AWT / Swing
    Replies: 4
    Last Post: 09-09-2008, 05:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •