Results 1 to 12 of 12
Thread: get string from a text file
- 08-08-2009, 11:30 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 32
- Rep Power
- 0
-
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.
- 08-08-2009, 02:03 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 32
- Rep Power
- 0
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.htmLast edited by Kruptein; 08-08-2009 at 02:30 PM. Reason: found a possible solution on my own
- 08-13-2009, 11:01 AM #4
Member
- Join Date
- Aug 2009
- Posts
- 32
- Rep Power
- 0
I think the problem is that it doesn't read the content of configpath, but only the name,
How can I solve this?
Kruptein
- 08-13-2009, 11:45 AM #5
Hi,
Use it like this and catch the "File Not Found Exception"
Scanner sc = new Scanner(new File(configpath));Ramya:cool:
- 08-13-2009, 12:52 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 32
- Rep Power
- 0
Okay now I've the foll. code:
No if I give the path to the config fileJava 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); } }
it only returns #*FileName=*config.ssa# in my textbox
config.ssa
It still doesn't give me the string after editor... :(Java Code:#*FileName=*config.ssa# editor: gedit filepath: /home/darragh/Documenten/D_MENU/files
- 08-13-2009, 02:34 PM #7
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:
- 08-13-2009, 04:26 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 32
- Rep Power
- 0
I can't be more specific, I want to get the string behind editor:
Like you can see in the code, I tried the if statement to see if it can find the word 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); } }
but Netbeans give me the foll. error:
Incompatible types, required: boolean
(error about the line if(sc.findInLine("editor:")) )
- 08-14-2009, 11:23 AM #9
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:
- 08-14-2009, 02:38 PM #10
Member
- Join Date
- Aug 2009
- Posts
- 32
- Rep Power
- 0
Okay so I currently have:
and config.ssa: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); } }
I'm trying to get the string after editor: (in this case gedit)Java Code:#*FileName=*config.ssa# editor: gedit filepath: /home/darragh/Documenten/D_MENU/files
Can you say how I should do this
(Sorry but I didn't understand whole your answer)
- 08-14-2009, 03:35 PM #11
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 } //classLast edited by RamyaSivakanth; 08-14-2009 at 03:38 PM.
Ramya:cool:
- 08-14-2009, 04:45 PM #12
Member
- Join Date
- Aug 2009
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
how to store string in text file
By santhosh_el in forum AWT / SwingReplies: 2Last Post: 04-03-2009, 06:21 AM -
How to write a string middle of a text file?
By loggen in forum New To JavaReplies: 5Last Post: 12-19-2008, 08:48 AM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
Searching a string from a text file using Swing Buttons
By pradeep1_mca@yahoo.com in forum AWT / SwingReplies: 2Last Post: 09-15-2008, 09:50 AM -
Searching a String from Text file using Swings .
By pradeep1_mca@yahoo.com in forum AWT / SwingReplies: 4Last Post: 09-09-2008, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks