Results 1 to 7 of 7
  1. #1
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Question filename as a string

    hi....


    is it possible to get a file name as a string..Im choosing a text file with a file chooser. the selected file name should be stored in a string..
    wat s the command to convert the file object to string.
    Java Code:
    boolean openFile() {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle("Open File");
    // Choose only files, not directories
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    // Start in current directory
    fc.setCurrentDirectory(new File("."));
    // Set filter for Java source files.
    fc.setFileFilter(fJavaFilter);
    // Now open chooser
    int result = fc.showOpenDialog(frm);
    if (result == JFileChooser.CANCEL_OPTION) {
    return true;
    }
    else if (result == JFileChooser.APPROVE_OPTION) {
    fFile = fc.getSelectedFile();
    [COLOR="DarkOrange"]aufile = toString(fc.getSelectedFile());[/COLOR]
    
    try {
    FileInputStream fstream = new FileInputStream(fFile);
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(
    new InputStreamReader(in));
    // Read File by char
    char[] buffer = new char[4096];
    int len=0;
                     while((len = br.read(buffer)) != -1) { // Read a batch of chars
                    String s = new String(buffer, 0, len); // Convert to a string
                    textArea1.setText(s);
    				sp = new JScrollPane(textArea1);
    				frm.add(sp);
    
                }
    here fFile is a file object..
    pls help me in fixing my errors..

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    And what do the API docs for File have to say about that?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Have you tried toString()? Or anything else? Or read through the File API documentation?

  4. #4
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Default filename to string

    ya i used filename.toString() ..its working.. thanks..

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    And, I can now guarantee that you didn't even look that API docs. Probably didn't even bother to go so far as to click on the provided link. You need to learn to use the API docs. This question is no question whatsoever. It takes two minutes max to find the answer in the API docs, instead you waited 12 minutes (and it probably took more than 2 minutes just asking the question) for someone to randomly see and read your question and decide to answer it. Now, which of those methods is more effecient? (And, to tell you the truth, the one that is more effecient, which isn't the route you took, is also less effort, as you see by this comparison.)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    ya i used filename.toString() ..its working.. thanks..
    You're welcome, of course.

    I'll add to what masijade said, that reading the API is not only a more efficient way of figuring out how to print filenames but it is about the only way of getting a decent feel for the varied behaviour of the gazillions of classes available.

    It's not that I'm suggesting that you read the docs from cover to cover, but (I find) when you look for one thing you almost always notice two or three other things. Sometimes you remember what you found! Over time, you learn the API. And learning is far more valuable thing than being able to print filenames.

    If you haven't done it already, bookmark the link masijade provided.

  7. #7
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
    aufile =fc.getSelectedFile().toString();

Similar Threads

  1. JFileChooser filename autocompletion
    By skorpionking in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2011, 05:01 PM
  2. New filename after try catch
    By hellzxmaker in forum New To Java
    Replies: 3
    Last Post: 11-05-2010, 11:07 PM
  3. Replies: 4
    Last Post: 04-03-2010, 06:20 PM
  4. Filename Filter problem
    By KevMeistr in forum Advanced Java
    Replies: 2
    Last Post: 06-14-2008, 02:43 AM
  5. Reading a directory and getting the filename
    By mrjunsy in forum Advanced Java
    Replies: 1
    Last Post: 05-10-2008, 02:36 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
  •