Results 1 to 10 of 10
  1. #1
    gezzel is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default how to use JFileChooser to select file -> string

    I'm trying to use jfilechooser to select a text file, and convert it to a string that i can manipulate. The file is going to be fairly large so i don't know if i need a buffer or anything.

    My end goal is to select a text file, format it by replacing certain strings, and then output the newly formatted text file.

    Thanks in advance

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    You forgot to post your code and explain what your problem is.

  3. #3
    gezzel is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    im' pretty new to java. My problem is i hve a JForm and the user selects the file. I stored the file as a file object...

    Java Code:
    private void FileChooserActionPerformed(java.awt.event.ActionEvent evt) {                                              
    
        int retval = FileChooser.showOpenDialog(null);
    if (retval == FileChooser.APPROVE_OPTION) {
        File input = FileChooser.getSelectedFile();
        toFormat = input.String();
    }
    I want to convert the file to a string so i can use Regex to find/replace instances of specific strings but i don't knwo how to manipulate/work with a File object...

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    String s = String.valueOf(File);

    might work.
    I die a little on the inside...
    Every time I get shot.

  5. #5
    gezzel is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    the file i'm trying to select is very large, ~160 megs. It's technically a csv file. not sure if this will cause any problems as i have to parse through the entire file to replace about 7 strings with different strings..

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    don't knwo how to manipulate/work with a File object
    Have you read the API doc for File. That is usually the best place to start. And often faster than asking someone here to read it to you. There are lots of methods that return different values. Write test code to use all of them to see which one is what you want.

    I want to convert the file to a string
    Does that mean you want to read the contents of the file to work on
    or do you want to work on the name of the file?

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    BufferedReader file = new BufferedReader(new FileReader(String));
    String s = file.readLine();
    and to write use
    BufferedWriter file = new BufferedWriter(new FileWriter(String));

    don't forget to use file.flush & file.close for both.

    Instead of String you also can use a file, I think.

    This is what you ment?
    I die a little on the inside...
    Every time I get shot.

  8. #8
    gezzel is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    i went over the doc but was confused about it...
    I'm trying to read the contents of the file(.csv file) and convert it to a string so i can use regular expressions to find/replace certain text in the line, then save the newly formatted file to a .txt

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    That's gonna be one hell of a string. Use a loop to compare it all line by line.
    I die a little on the inside...
    Every time I get shot.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    There are plenty of code examples on this forum that show how to read a file. Do a Search for BufferedReader for example.

Similar Threads

  1. [SOLVED] jfilechooser for saving a file....?
    By prabhurangan in forum AWT / Swing
    Replies: 10
    Last Post: 10-06-2013, 11:17 PM
  2. how to use JFileChooser to select file -> string
    By gezzel in forum AWT / Swing
    Replies: 15
    Last Post: 10-23-2008, 06:34 AM
  3. Cannot display a random string from .dat file
    By explosion242 in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 02:48 PM
  4. can i update the tag value of XMl file by other value(string
    By pankaj_salwan in forum Advanced Java
    Replies: 7
    Last Post: 07-04-2008, 09:12 AM
  5. Read a string from a txt file
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:50 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
  •