I have a situation where i have a txt file and java file bundled inside jar. I am reading txt file from java which is bundled inside jar only.
While reading file, getting FileNotFoundException in Java and where as txt file is in same folder bundled inside jar.
I am calling this Java method from a test class sample code.
public static void loadtxtfile(){
try {
InputStream in =
Utils.class.getClassLoader().getResourceAsStream("sample.txt");
File f = new File(JetUtils.class.getClassLoader().getResource("dd.js").getFile());
//OJetBase.class.getClassLoader().getResourceAsStream("logging.properties");
BufferedReader input = new BufferedReader(new FileReader(js_filepath));
StringBuffer buffer = new StringBuffer();
while ((text = input.readLine()) != null)
buffer.append(text + "\n");
java_script = buffer.toString();
}
Test call - Utils.loadtxtfile();
I tried all the options.
You cannot read resources from a jar file as a java.io.File object because they do not exist in a file system. Just use java.lang.Classloader.getResourceAsStream(String name):
Reader inputStreamReader = new InputStreamReader(
JetUtils.class.getClassLoader().getResourceAsStream("dd.js"));
BufferedReader jsReader = new BufferedReader(inputStreamReader);
StringBuilder javascript = new StringBuilder();
String input;
while ((input = jsReader.readln()) != null) {
javascript.append(input);
}
Related
So I wrote this file reader method that should return a string of everything that is in the file, but it isn't working properly. Writing into the file works perfectly, but this reading method doesn't. What the method does currently is it reads the last string/text added, but it does not read the file from start to finish. 'br' is my bufferedReader, which is declared somewhere else in the same class.
Here's how br is defined:
private static FileInputStream fis;
private static BufferedReader br;
and then in the constructor:
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
Here's the method:
public String readStuff(){
String line = "";
String r = "";
try{
while((line = br.readLine()) != null){
System.out.println(line + " read ");
r+= line;
}
//br.close(); JDK 7 does this automatically apparently
}catch(IOException e){
e.printStackTrace();
System.out.println("Error at readStuff!");
}
return r;
I know I'm making either a logic mistake or some obvious error, I just don't know where.
If you want to read the entire file twice, you will have to close it and open new streams/readers next time.
Those streams/readers should be local to the method, not members, and certainly not static.
Using File and FileReader You can Read / Write File From Dir.
you can get File using File class object
File file = new File("file.txt");
and After Process to read that file
FileReader fr = new FileReader(file);
There are Whole Code to read File...
File file = new File("G:\\Neon\\data.txt");
FileReader fr = new FileReader(file);
String data = "";
while((i = fr.read()) != -1)
{
data = data + (char)i;
}
System.out.println(data);
Here it is my folder project
I would like to read the file book-form.html which is in the directory web of my project and put it in a String.
This is how I call my function 'getFileContent':
String content = getFileContent("web/book-form.html");
And this is the function:
public String getFileContent(String filePath){
String line, content = new String();
try {
File file = new File(filePath);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null){
content += line;
}
br.close();
fr.close();
} catch(IOException e){
System.out.println(e.getMessage());
}
return content;
}
My problem is that netbeans tell me that it cannot find my file book-form.html
Any ideas ?
File path to resource in our war/WEB-INF folder?
Also you should close stream in a final block or use try-with-resource if you use jdk 7+
I find the way to do it:
Basically the program is in the main folder of Glassfish, so it's needed to put the entire path of your file from the root of your system to allow the program to find your file.
I am trying to read a text file from inside of exectutable jar. I can read it from eclipse, but can not from executable jar.I googled, and found out I have to use getClass.getResource. But all google examples are not for Buffered Reader.
My current codes are the following.
BufferedReader in = null;
try
{
File file = new File ("tfl.txt");
in = new BufferedReader(new InputStreamReader( new
FileInputStream(file),"unicode"));
...
}
Try this
InputStream localInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("resource_name");
BufferedReader br = new BufferedReader(new InputStreamReader(localInputStream));
I need to read contents of a file as a server, and then send the read data file, for the client so the client print it out on the Client terminal.
The problem is that I can't find a way or method to read a txt file from the current directory which my java file and txt file are existed.
Please help me.
There are many ways to read text file or file in java. It depend on you to that in which format you need to pass your file content to client side.
Here are some method to reading file in java.
1. Using BufferedReader class
BufferedReader input = new BufferedReader(new FileReader(aFile));
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
String curLine = line;
//Process line
}
2.Using Apache Common IOUtils with the class IOUtils.toString() method.
FileInputStream inputStream = new FileInputStream("FILEPATH/FILENAME");
try {
String everything = IOUtils.toString(inputStream);
} finally {
inputStream.close();
}
3.Using the Scanner class in Java and the FileReader
Scanner in = new Scanner(new FileReader("FILENAME/FILEPATH"));
while (scanner.hasNextLine()){
//process each line in some way
String line = scanner.nextLine();
}
Scanner has several methods for reading in strings, numbers, etc...
4.In JAVA 7 this is the best way to simply read a textfile
new String(Files.readAllBytes(...))
or Files.readAllLines(...)
Path path = Paths.get("FILENAME");
List<String> allLines = Files.readAllLines(path, ENCODING);
Please refer this link for more onfomation.
You can use BufferedReader to read from a txt file.
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
here fileName is a string that contain your absolute file name.
eg : fileName = "C:\temp\test.txt";
You can read file by using BufferedReader.
File file=new File("filepath");
BufferedReader br=new BufferedReader(new FileReader(file)); //Here you create an object of bufferedreader which file read through filereader
String data=br.readLine();
while(data!=null)
{
System.out.println(data); // Writing in the console
data=br.readLine();
}
This will taking input from file and giving output to console.If you want it write in other file then use BufferedWriter.
File out=new File("outputfilepath");
BufferedWriter bw=new BufferedWriter(new FileWriter(out));
simply us bw.write() instead of System.out.println();.
I have to make a project for school; it's a game. I load the map from a text file. Currently I do it with a scanner, but I can't manage to get it working in a Runnable JAR file without putting the res file next to the JAR file. I want to get the text file inside; it worked with BufferedImages, but the text file doesn't work. I have this code:
public String ReadTextFile(String path) throws IOException {
String HoldsText= null;
FileReader fr = new FileReader(getClass().getResource(path).toString());
BufferedReader br = new BufferedReader(fr);
while((HoldsText = br.readLine())!= null){
System.out.println(HoldsText);
}
return HoldsText;
}
path = "res/Maps/Map2.txt"
error:
java.lang.NullPointerException
at aMAZEing.TextManager.ReadTextFile(TextManager.java:22)
at aMAZEing.Map.openFile(Map.java:89)
at aMAZEing.Map.<init>(Map.java:31)
at aMAZEing.Board.<init>(Board.java:50)
at aMAZEing.Maze.<init>(Maze.java:24)
at aMAZEing.Maze.main(Maze.java:15)
file structure: http://speedcap.net/sharing/screen.php?id=files/a9/77/a977e8b487f21e67db941a96087561cd.png
This doesn't seem to work though. I've researched a lot but could not find anything that worked for me. I just need the whole text file in a string, the rest is easy with substring and so on.
EDIT!:
The resolution to this was that my path had res in it, and it didn't work because of that. I deleted the res and got "/Maps/Map2.txt" as path, now the file loads and my map is displayed again.
public static String ReadTextFile(String path) throws IOException{
String HoldsText= null;
InputStream is = getClass().getResourceAsStream(path);
InputStreamReader fr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
while((HoldsText = br.readLine())!= null){
sb.append(HoldsText)
.append("\n");
}
return sb.toString();
}
You need to append the lines and use InputStreamReader instead of FileReader