Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

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

  2. #2
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    It's very possible. Try looking at the FileReader and FileWriter APIs.

    Here's a quick example:


    Java 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-06-2007 at 11:44 PM.

  3. #3
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    Eh. Are there code-formatting tags available?

  4. #4
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    no what i meant is opening as in double clicking the txt file. i don't mean editing it in java.

  5. #5
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    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

  6. #6
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    thanks. your a life saver

  7. #7
    levent Guest

    Default

    Eh. Are there code-formatting tags available?
    Yes. Check your previous message. I placed it inside [code] tag.

  8. #8
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    You're welcome!

    Joe

  9. #9
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    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?

  10. #10
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    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:

    Java 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

  11. #11
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    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.

  12. #12
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    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

  13. #13
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    ok umm, i have no idea how to use the code u gave me.

  14. #14
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    it doesn't compile with a class.

  15. #15
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    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.

    Java 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();
    }
    }

  16. #16
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    when u say path to source file, and path to destination file, what do u mean?

  17. #17
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    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

  18. #18
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    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

  19. #19
    Join Date
    Aug 2007
    Posts
    25
    Rep Power
    0

    Default

    It won't compile because you have a space between the ! and the = in the operatior !=.

  20. #20
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default

    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

Page 1 of 2 12 LastLast

Similar Threads

  1. open a pdf file in linux
    By aparna in forum Advanced Java
    Replies: 5
    Last Post: 06-08-2010, 12:51 PM
  2. how to open a file through URL
    By katie in forum New To Java
    Replies: 3
    Last Post: 07-13-2008, 02:53 AM
  3. Open Blue Lab 1.3.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-19-2007, 03:37 PM
  4. open lazlo
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 06:22 AM
  5. Open Blue Lab 1.2.17
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-07-2007, 08:57 AM

Posting Permissions

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