Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2007, 01:16 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
Is it possible to open a txt file?
I am currently trying to make a database, and im storing all the info in a txt file. I want to user to be able to open the txt file and edit any line they want. Just wondering, but is it possible to open a txt file with java? if not, can you give me some ideas.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 01:25 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
It's very possible. Try looking at the FileReader and FileWriter APIs.

Here's a quick example:


Code:
BufferedReader br; try { br = new BufferedReader(new FileReader("test.txt")); int ch; while((ch = br.read()) != -1) dostuff(ch); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch(IOException ex) { ex.printStackTrace(); }

Last edited by levent : 08-07-2007 at 01:44 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 01:28 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
Eh. Are there code-formatting tags available?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-07-2007, 01:32 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
no what i meant is opening as in double clicking the txt file. i don't mean editing it in java.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-07-2007, 01:40 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
Ah. Ok.

Well, you'll have to provide some means of accessing the file. For example, you can use a JFileChooser to allow the user to browse to the file, and upon opening it, you can use a FileReader to read it into a Document or a String, and then add that Document or String to a text widget (JTextArea, JEditorPane, etc.).


Here are some resources that I hope will help you:

JFileChooser tutorial:
How to Use File Choosers (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

Text Component tutorials:

Using Text Components (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

Good luck!

Joe
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-07-2007, 01:44 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
thanks. your a life saver
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-07-2007, 01:45 AM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Quote:
Eh. Are there code-formatting tags available?
Yes. Check your previous message. I placed it inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-07-2007, 01:45 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
You're welcome!

Joe
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-07-2007, 01:48 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
actually, never mind. its kinda too hard. to incoporate 3 classes together, and i don't know how. Can you tell me how to edit a specific line or word to another in a txt file?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 08-07-2007, 01:54 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
Um. You can edit the file stream directly. Here's an example that will replace every instance of one word with an instance of another:

Code:
BufferedReader br = new BufferedReader(new FileReader("<path to source file>")); BufferedWriter bw = new BufferedWriter(new FileWriter("<path to destination file>")); String str; while((str = br.readLine()) != null) { str.replaceAll("oldword", "newword"); bw.write(str); } br.close(); bw.flush(); bw.close();
I sincerely hope I'm not doing your homework for you. Sorry, but my time on the Sun Java Forums has made me skeptical :/

Joe

P.S. Thanks for the code hint
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 08-07-2007, 02:03 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
na, my homework has only about 100 more lines of coding to do. im trying to make a database, and the code you just told me is suppose to help me edit the last name and address of a person incase they are married.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 08-07-2007, 02:06 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
Ok. Good luck. I don't know that such an indiscriminate find-and-replace method will work very well, but I hope it's useful/gives you an idea about file IO.

Joe
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 08-07-2007, 02:09 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
ok umm, i have no idea how to use the code u gave me.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 08-07-2007, 02:09 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
it doesn't compile with a class.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 08-07-2007, 02:14 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
Ok. Firstly, the code needs to throw or catch several exceptions, specifically a FileNotFoundException and an IOException (Although the IOException will catch the FileNotFoundException, catching the FileNotFoundException will give you a better idea of what's going wrong).

So, create a class, e.g.

Code:
public class StringReplace { public StringReplace() //Constructor { } public void replaceString() throws FileNotFoundException, IOException { BufferedReader br = new BufferedReader(new FileReader("<path to source file>")); BufferedWriter bw = new BufferedWriter(new FileWriter("<path to destination file>")); String str; while((str = br.readLine()) != null) { str.replaceAll("oldword", "newword"); bw.write(str); } br.close(); bw.flush(); bw.close(); } }
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 08-07-2007, 02:33 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
when u say path to source file, and path to destination file, what do u mean?
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 08-07-2007, 02:40 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
Find the location of the file on the disk, ex. "C:\Documents and Settings\testfile.txt" and, in its place in the program, enter "C:\\Documents and Settings\\testfile.txt".

Joe
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 08-07-2007, 02:53 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
just wondering, but why won't this compile.

CODE:

import java.io.*;

public class DB{
String s = "DataBase.txt";
public static void display(String s) throws IOException{
FileReader fr = new FileReader(s);
BufferedReader br = new BufferedReader(fr);
String line = br.readline();
while (line ! = null){
line = br.readline();
}
br.close();
fr.close();
}

public void find(){
String phrase = line;
String word = JOptionPane.showInputDialog (null, "Please enter the word you are looking for", "Find", JOptionPane.OK_CANCEL_OPTION);
}
}


ERROR:

[line: 9]
Error: ')' expected
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 08-07-2007, 03:11 AM
Member
 
Join Date: Aug 2007
Posts: 25
Josiah.Haswell is on a distinguished road
It won't compile because you have a space between the ! and the = in the operatior !=.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 08-07-2007, 03:15 AM
Member
 
Join Date: Jul 2007
Posts: 18
jason27131 is on a distinguished road
k i did that, but more errors come up.

CODE

import java.io.*;

public class DB{
public static void display(String s) throws IOException{
FileReader fr = new FileReader(s);
BufferedReader br = new BufferedReader(fr);
String line = br.readline();
while (line != null){
line = br.readline();
}
br.close();
fr.close();
}
}

ERROR

File: C:\Documents and Settings\David\Desktop\jason's homework. DO NOT DELETE\Java Programming\DB.java [line: 7]
Error: cannot find symbol
symbol : method readline()
location: class java.io.BufferedReader
File: C:\Documents and Settings\David\Desktop\jason's homework. DO NOT DELETE\Java Programming\DB.java [line: 9]
Error: cannot find symbol
symbol : method readline()
location: class java.io.BufferedReader
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
open a pdf file in linux aparna Advanced Java 4 09-28-2008 07:37 AM
how to open a file through URL katie New To Java 3 07-13-2008 04:53 AM
Open Blue Lab 1.3.0 JavaBean Java Announcements 0 07-19-2007 05:37 PM
open lazlo Peter Advanced Java 2 07-04-2007 08:22 AM
Open Blue Lab 1.2.17 levent Java Announcements 0 06-07-2007 10:57 AM


All times are GMT +3. The time now is 10:35 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org