Discussion:
Recognizing a .txt file
mkayman1
2012-07-01 22:56:05 UTC
Permalink
I put a a .txt file inside a package, but whenevr I put my mouse on the file, it says "unrecognized file". And I cant write anything on the file using BufferedWriter.

Can anyone tell me how to solve this?
Llelan D.
2012-07-02 18:40:42 UTC
Permalink
Post by mkayman1
I put a a .txt file inside a package, but whenevr I put my mouse on the file, it says "unrecognized file". And I cant write anything on the file using BufferedWriter.
When you hover your mouse over the file in the NetBeans project explorer, it brings up the tooltip "Unrecognized File" because NetBeans does not recognize that file as any source code or project file, so interpretation of the file when opened falls to the global formatting configuration.

When you place a file into a project package, it gets packaged into the project jar file in the same directory as the class files for that package and should be considered a read-only resource.

When you try to open a filename with no directory path from your code, the file is opened/created in the current directory. The current directory is from wherever you ran the jar file, or the project directory if you ran your code in NetBeans, and not the package directory within the project jar file. In NetBeans, you can see your project directory in the Files tab of the explorer panel on the left (Not the Projects tab).

You can read a file in a package from the project jar file (a package resource) using the getResourceAsStream() method of any class in that package.

Code:
public class Main {
/**
* Application entry.
*
* @param args The array of command line argument Strings.
*/
public static void main(String[] args) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream("test.txt")))) {
while (true) {
String line = in.readLine();
if (line == null) break;
System.out.println(line);
}
} catch (IOException exception) {
System.out.println(exception.getLocalizedMessage());
}
}
}

Loading...