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));
Related
I am trying to open a txt file in a java program and the compiler keeps advice me throwing the error, that is responsible for the program crash:
FileNotFoundException
I am working on intelliJ and the file is in the same directory I am working on, it is in the src directory.
The file is called dataset.txt and I tried all of the below formats to solve the issue.
BufferedReader br = new BufferedReader(new FileReader("D:\\Mhamed\\Courses\\Algorithms Specialization\\Course 1\\Week 2\\src\\dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("/dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("./dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("src\\dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("D:/Mhamed/Courses/Algorithms Specialization/Course 1/Week 2/src/dataset.txt"));
Finally, nothing works and I don't know what is the issue. Can someone help me?
Make sure to put the file in the resources folder, that way you can use:
InputStream in = Reader.class.getClassLoader().getResourceAsStream("dataset.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in)); //read file
int lines = 0;
while (br.readLine() != null) lines++; //example to count lines
You then can apply code to every line of the file.
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);
}
I am having trouble reading from a file. Here is my code can anyone show me where I am wrong?
public static Map<Route, List<Service>> read(String fileName)
throws IOException, FormatException {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String strLine;
while((strLine = reader.readLine())!= null)
{
/* Own Code */
}
reader.close();
}
I am having a FileNotFound Exception. May this be a the location of my file that is wrong?
You seem to want to use a resource. A resource is not accessed as a file, it is better to use it as a stream.
InputStream resourceStream = MyClass.class.getResourceAsStream(fileName);
BufferedReader myReader = new BufferedReader(new InputStreamReader(resourceStream));
Above code takes the location of your class in account, so you can simply use the fileName as is, without a path, and place the fileName next to your .java file. It will automatically be placed next to the generated .class files and - when packaged - in your .jar file.
Just as owlstead commented keep in appropriate location and try like this
URL url = ClassLoader.getSystemResource(fileName);
br = new BufferedReader(new InputStreamReader(url.openStream()));
i.e keep the file in classes folder or bundle with jar or current working directory etc.
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 am using following code to write json to my local path which i get from my html page.Again I have to construct a html page by reading content from the saved local json file.For this I have to read this saved file from local which is plain text and give as input to java file. I got confused whether to use Buffered Reader or BufferedInputStream to read that file from local path.Please help me.
java.io.BufferedWriter jsonOut = new java.io.BufferedWriter(
new java.io.OutputStreamWriter(
new java.io.FileOutputStream(uploadDir +
_req.getParameter("filename")), "ISO-8859-1"));
BufferedReader for text.
Reason: http://tutorials.jenkov.com/java-io/bufferedreader.html
You can use BufferedReader for text but you should ensure to use the proper charset in your case (otherwise it defaults to the platform charset)
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(myFile),"ISO-8859-1"));
To read a file you can use the following code
File f = new File("your json file");
BufferedReader buf = new BufferedReader(new FileReader(f));
String line = null;
while ((line = buf.readLine()) != null) {
System.out.println("json file line " + line);
// do your changes
}