|
|
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.
|
|

08-07-2007, 01:16 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
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.
|
|

08-07-2007, 01:25 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
It's very possible. Try looking at the FileReader and FileWriter APIs.
Here's a quick example:
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.
|
|

08-07-2007, 01:28 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
|
Eh. Are there code-formatting tags available?
|
|

08-07-2007, 01:32 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
no what i meant is opening as in double clicking the txt file. i don't mean editing it in java.
|
|

08-07-2007, 01:44 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
thanks. your a life saver
|
|

08-07-2007, 01:45 AM
|
|
Senior Member
|
|
Join Date: Dec 2006
Posts: 748
|
|
Eh. Are there code-formatting tags available?
Yes. Check your previous message. I placed it inside [code] tag.
|
|

08-07-2007, 01:45 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
|
You're welcome!
Joe
|
|

08-07-2007, 01:48 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
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?
|
|

08-07-2007, 01:54 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
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:
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
|
|

08-07-2007, 02:03 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
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.
|
|

08-07-2007, 02:06 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
|
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
|
|

08-07-2007, 02:09 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
ok umm, i have no idea how to use the code u gave me.
|
|

08-07-2007, 02:09 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
it doesn't compile with a class.
|
|

08-07-2007, 02:14 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
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.
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();
}
}
|
|

08-07-2007, 02:33 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
when u say path to source file, and path to destination file, what do u mean?
|
|

08-07-2007, 02:40 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
|
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
|
|

08-07-2007, 02:53 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
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
|
|

08-07-2007, 03:11 AM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 25
|
|
|
It won't compile because you have a space between the ! and the = in the operatior !=.
|
|

08-07-2007, 03:15 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 18
|
|
|
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|