Java Filenotfound exception in jar file - java

i wrote a program, but when my friends try to execute it it throw filenotfound exception, but the file is exist, here is my code, and in the folder have lib folder, the jar file and the "csv fajlok" and in the csv fajlok folder there is the 2 csv file
String csvFile = "csv fajlok\\pontcsoport.csv";
BufferedReader br = null;
String line = "";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] pontGroupLine = line.split(";");
String[] price_split = pontGroupLine[1].split(" ");
try{
doubleDTList.add(Double.parseDouble(price_split[0]));
}catch(NumberFormatException e){
}
}
}catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Nem található a pontcsoport fájl (/csv fajlok)");
}catch (IOException e) {
}finally {
if (br != null) {
try {
br.close();
}catch (IOException e) {
}
}
}

If the file is outside the jar file, you should put an absolute path, or the "csv fajlok" folder should be in the same folder where you execute the jar file.
If the file is inside the jar file, you cannot access to it as a file but as a Stream, with the method Class.getResourceAsStream(String path).

Better to use Java NIO2 to read the file content:
List<String> lines = Files.readAllLines(Paths.get("path-to-file"), charset);
It's allow to avoid using while loop with redundant readers.
What is the OS installed on failed notebooks?
Also try to change folder name using '_' instead of space. I think it's the main reason of the issue.

Related

How do I read all the contents of a HTML file inside of my jar and store it in a string

I have an HTML file under a directory in my JAR file under pages/newTab.html, and in Eclipse it obviously is in res/pages/newTab.html. My goal here is to read the contents of the whole HTML file and store it in a String.
I have tried multiple ways to get the file as a resource, then read it with a BufferedReader, it works in Eclipse, but not in the Runnable JAR. This is normally how I get the file as a resource, this works in Eclipse, but not in the Runnable JAR.
getClass().getResource("/pages/" + url.substring(7))
Here is what I have so far.
File file = new File(getClass().getResource("/pages/" + url.substring(7)).toURI());
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
jep.setContentType("text/html");
jep.setText(stringBuffer.toString());
jep is the JEditorPane we are printing the text to.
EDIT: I do get an java.lang.IllegalArgumentException: URI is not hierarchical when Running the Runnable JAR with File file = new File(getClass().getResource("/pages/" + url.substring(7)).toURI());
Save the html outside the jar, this is what you can do with configuration files and other stuff like that. Create a folder for the application and store the executable jar and html inside.
- application\
------- executable.jar
------- htmlFile.html
now do something like this:
try {
List<String> lines = new ArrayList<>(Files.readAllLines(Paths.get("htmlFile.html"))));
String content = "";
for (String line : lines) {
content += line + "\n";
}
jep.setContentType("text/html");
jep.setText(content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Cannot find file in netbeans + glassfish project

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.

Cannot rename temp.txt to original.txt after removing line

I have the following code I've placed into a custom method.
File inputFile = new File("F:\\EmployeePunch\\EmployeePunch\\src\\employeepunch\\original.txt"); // Your file
File tempFile = new File("F:\\EmployeePunch\\EmployeePunch\\src\\employeepunch\\temp.txt");// temp file
BufferedReader reader = null;
BufferedWriter writer = null;
try{
reader = new BufferedReader(new FileReader(inputFile));
writer = new BufferedWriter(new FileWriter(tempFile));
String firstName = chosenEmployee.getFirstName();
String lastName = chosenEmployee.getLastName();
String currentLine;
while((currentLine = reader.readLine()) != null) {
if(currentLine.contains(firstName)
&& currentLine.contains(lastName)) continue;
writer.write(currentLine);
}
}
catch (IOException e) {
e.printStackTrace();
}finally{
try {
inputFile.delete();
writer.close();
boolean successful = tempFile.renameTo(inputFile);
System.out.println(successful);
} catch (IOException ex) {
ex.printStackTrace();
}
}
The problem is; whenever I run the program the renameTo fails; but it does create a temp.txt with the line that was supposed to be removed correctly removed.
Why does renameTo always return false though?
Edit 1: The files are not opened anywhere else in windows. They are listed in my IDE's project explorer but are not opened by my IDE.
The javadoc for renameTo() says:
The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.
Try closing all the files (you are not closing reader so it is still "in use") and deleting first the input file:
} finally {
try {
inputFile.delete();
reader.close();
writer.close();
inputFile.delete();
boolean successful = tempFile.renameTo(inputFile);
System.out.println(successful);
} catch (IOException ex) {
ex.printStackTrace();
}
}

Unable to add text file in java eclipse during creation of jar

I have created a java project in eclipse and added certain text files like follows
FileReader fin=null;
BufferedReader bin=null;
fin=new FileReader("src/main/resources/League.txt");
bin=new BufferedReader(fin);
But after creation of the ruunable jar or just simple jar when I run the jar file it is showing that no text file is found or the path is not found. But I have added the text files in the main.resource of my project. How to handle it?
Use URLClassLoader.getSystemResourceAsStream, this works in jars and in eclipse...
Make sure there's a file in the latest Java version directory src\main\resources\League.txt. For example, say for Windows, C:\Program Files\Java\jdk1.7.0_25\src\main\resources\League.txt.
You need to give front slashes, not back ones. Also, this should be the code:
FileReader fin=null;
BufferedReader bin=null;
fin=new FileReader("src\\main\\resources\\League.txt"); // Because of Unicode restrictions.
bin=new BufferedReader(fin);
Maybe you check this as well: How to get a path to a resource in a Java JAR file
Try this. Even though the file is in src/resources you need not say src folder in path.
BufferedReader br = null;
try {
String sCurrentLine;
URL url = ClassLoader.getSystemResource("resources/League.txt");
br = new BufferedReader(new InputStreamReader(url.openStream()));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

How to write in specific place inside a file using ant

i am using java ant to deploy my application . I have a file app.php . I want to write some text in app.php while deploying in a specific location inside that file . This is my app.php :
'providers' => array(
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
),
I want to add a line at the end of this line :
'Illuminate\Workbench\WorkbenchServiceProvider',
Please tell me how to do this .
Thanks.
you must use subString method like this:
at first store your file in a String so after that you could do this:
String s="your file";
String firstPart=s.substring(0,s.lastIndexOf(")")+1);
String lastPart=s.substring(s.lastIndexOf(")")+1);
firstPart=firstPart+"\n"+"'Illuminate\\Workbench\\WorkbenchServiceProvider',";
s=firstPart+lastPart;
How to read from file ?
Use BufferedReader to wrap a FileReader
BufferedReader br = null;
StringBuilder sb=new StringBuilder();
try {
String line;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((line= br.readLine()) != null) {
sb.append(line+"\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
String str=sb.toString();
See the Replace or Filter ant tasks.

Categories

Resources