0

I am new in Java and facing a problem aceessing a file. I have a package named "FileOperation" uses a "boom.txt" file(Location: "FileOperation/files/boom.txt"). What I wanted to do was to access the file from a class "MyTextPanel" (Location: "FileOperation/MyTextPanel.java").

I have created 2 methods for this purpose. One is by getClass.getResource(path) and other is by giving the absolute location of the file. Second method works, first doesn't. Someone please explain, what's the problem with the first method. Here is the class MyTextPanel:

package FileOperaton;
import java.awt.BorderLayout;
import java.io.FileReader;
import java.net.URL;

import javax.swing.JPanel;
import javax.swing.JTextArea;


public class MyTextPanel extends JPanel {

    JTextArea textArea;
    MyTextPanel()
    {
        setLayout(new BorderLayout());
        textArea=new JTextArea();
        add(textArea,BorderLayout.CENTER);

        String fileName;
        fileName=getFileName1();
        //fileName=getFileName2();
        System.out.println("File Name = "+fileName);
        if(fileName!=null)
        {
            try {
                    FileReader fr=new FileReader(fileName);
                    System.out.println("Access Successful");
            } catch (Exception e) {
                    e.printStackTrace();
            }
        }
    }

    private String getFileName1()
    {
        URL url=getClass().getResource("/files/boom.txt");
        System.out.println("by getFileName1()");
        if(url!=null)
            return url.getFile();
        return null;
    }

    private String getFileName2()
    {
        String absolutePath="E:\\Project Eclipse\\Workspace\\FileOperation\\src\\FileOperaton\\files\\boom.txt";
        System.out.println("by getFileName2()");
        return absolutePath;
    }
}

And here are the outputs:

by getFileName1()
File Name =/E:/Project%20Eclipse/Workspace/FileOperation/bin/FileOperaton/files/boom.txt
java.io.FileNotFoundException: E:\Project%20Eclipse\Workspace\FileOperation\bin\FileOperaton\files\boom.txt (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileReader.<init>(Unknown Source)
    at FileOperaton.MyTextPanel.<init>(MyTextPanel.java:32)
    at FileOperaton.MyFrame.<init>(MyFrame.java:22)
    at FileOperaton.MyFrame.main(MyFrame.java:29)

by second method:

by getFileName2()
File Name = E:\Project Eclipse\Workspace\FileOperation\src\FileOperaton\files\boom.txt
Access Successful

I've also tried url=getClass().getResource("/files/boom.txt") in the first method but there url is assigned as null. And in the second method, if I use absolutePath="E:\Project Eclipse\Workspace\FileOperation\bin\FileOperaton\files\boom.txt"; (bin instead of src) it also gets success.

FRR
  • 99
  • 8

3 Answers3

0

Carefully read your output:

First Method:

E:\Project%20Eclipse\Workspace\FileOperation\bin\FileOperaton\files\boom.txt

Second Method:

E:\Project Eclipse\Workspace\FileOperation\src\FileOperaton\files\boom.txt

One time you have the file in the src folder, one time in the bin folder.

I would recommend to make a third folder res and store it there (E:\Project Eclipse\Workspace\FileOperation\res\boom.txt) and the load it like this:

public static URL getFile (String filename)
{
    return MyTextPanel.class.getClassLoader().getResource(filename);
}
FRR
  • 99
  • 8
msrd0
  • 7,816
  • 9
  • 47
  • 82
  • Thanks for your answer. How to write the getFileName1() method now?. I tried like this: private String getFileName1() { URL url=getFile("/res/boom.txt"); System.out.println("by getFileName1()"); if(url!=null) return url.getFile(); return null; }. It doesn't give success. I have also tried "res/boom.txt","/boom.txt","boom.txt","FileOperation/res/boom.txt","/FileOperation/res/boom.txt". But same result. File name is printed as null.Is this could be a compiler problem? – FRR Oct 05 '14 at 16:38
  • @FRR Doesn't my method returns a valid URL if you call it like `getFile("res/boom.txt")`? – msrd0 Oct 05 '14 at 16:44
  • No @msrd0, it returns null. But the absolute name ""E:\\Project Eclipse\\Workspace\\FileOperation\\res\\boom.txt" works. – FRR Oct 05 '14 at 16:58
  • @FRR, it returns null because your should have written URL url=getClass().getResource("/FileOperation/files/boom.txt"); – Hai Bi Oct 05 '14 at 17:21
  • @Hai bi, I was saying, msrd0's method returns null. It was the getFile(String fileName) method. – FRR Oct 05 '14 at 18:50
0

Here is what I would do if files are in the FileOperation package/folder:

YourClass.class.getResource("/FileOperation/boom.txt");

And here is what you should do when your files are in FileOperation/files folder:

YourClass.class.getResource("/FileOperation/files/boom.txt");

Also, you can look into the bin folder to see whether the boom.txt is there. If not, probably refresh the project using F5 so the files/boom.txt is part of the project.

Hai Bi
  • 1,173
  • 1
  • 11
  • 21
  • Note the difference between `Class.getResource()` and `ClassLoader.getResourc()` – msrd0 Oct 05 '14 at 16:44
  • Yes, that may work. But I wanted to keep my resources well organized. So, sources and resources in the same folder is not a good choice for me. Any idea, why my getFileName1() is not working? – FRR Oct 05 '14 at 16:53
  • @FRR, I think since your should put FileOperation inside the String you pass to the getResource. – Hai Bi Oct 05 '14 at 17:10
0

Well, I have found a solution to my own problem. That could be helpful for others. Doing experiments with the "File Name :" output I figured out that, return of getFile1() and getFile2() actually differs with only a "sapce" charecter.That is "...Project%20Eclipse..."[returned by getFile1()] and "...Project Eclipse..."[returned by getFile2()].So, I have edited the getFile1() method:

private String getFileName1()
    {
        URL url=getClass().getResource("files/boom.txt");
        System.out.println("by getFileName1()");
        if(url!=null)
            return url.getFile().replaceAll("%20", " ");//<--- replace %20 by space charecter
        return null;
    }

And Finally it worked. If someone knew, why url generated spaces as %20 and why this wasn't working,that would be better.:)

FRR
  • 99
  • 8