I am getting the exception java.io.FileNotFoundException: D:\Selenium Reports\Daily Reports\Merged file.xls when I am trying to access excel file through java program.
I have tried with:
absolute path
relative path
read/write access to the file
checked whether the file is open
Still, it is not working, is it not working because I have guest access to the machine?
keep the .java file and the .xls file in the same folder and use the code:
try {
File f = new File("Merged file.xls");
if (!f.exists()) {
System.out.println("File does not exist");
if (!f.createNewFile())
System.out.println("File cannot be created");
else
System.out.println("File created");
} else {
System.out.println("File exists");
if(!f.canRead())
System.out.println("Error in reading. Need permission");
if(!f.canWrite())
System.out.println("Error in writing. Need permission");
}
} catch (IOException e) {
e.printStackTrace();
}
}
Related
I've been struggling to create a CSV file from my app. Here's my code:
private void createFile() throws IOException {
Log.i("test", "creating file");
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/test.csv");
if (!file.createNewFile()) {
Log.i("test", "file doesn't exist");
try {
Log.i("test", "gonna create the new file");
Log.i("test", "done :)");
} catch (IOException e) {
e.printStackTrace();
}
}
if (!file.exists()) {
Log.i("test", "Oh no");
return;
}
}
The Logcat prints:
creating file
file doesn't exist
gonna create the new file
Oh no
I don't know why file.createNewFile() returns false yet when I check if it exists, it also returns false.
I have my permissions declared in my Manifest and enabled storage permissions from settings as well.
I also have a function that checks if I have storage perms and it says that I do.
I have a java code that automatically create a file for the user after login. It works properly when I run it in netbeans/intellij. However when I compiled it into a jar file, the program works but the file is not created. Can someone please help me?
Here is my code:
private void createTxtFile(){
String loc = new File("").getAbsolutePath();
String dir = loc+"/src/lists/"+user+"-"+pass+".txt";
try {
File myObj = new File(dir);
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
}
else {
JOptionPane.showMessageDialog(this, "User already have an existing list file.");
}
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "An error occured in creating file for your list.", "Error",JOptionPane.ERROR_MESSAGE);
}
}
I am using Apache POI on android to export an excel file for my offline database. While creating a file instead of creating a .xls file its generates directories
//filename = "collectionreport.xls"
String path = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+File.separator+fileName;
File file = new File(path);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
workbook.write(fileOutputStream);
Log.e(TAG, "Writing file" + file);
isSuccess = true;
} catch (IOException e) {
Log.e(TAG, "Error writing Exception: ", e);
isSuccess = false;
} catch (Exception e) {
Log.e(TAG, "Failed to save file due to Exception: ", e);
isSuccess = false;
}
error:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/aa.bb.com.modules.tabbank/files/Download/CollectionReport163548565.xls (Is a directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:287)
at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)```
EDIT: It automatically worked after restarting my computer running on linux and re installing the application
I try to upload a folder to Dropbox in my android code by this code:
try {
// Upload to Dropbox
InputStream inputStream = new FileInputStream(file);
dbxClient.files().uploadBuilder("/" + file.getName()) //Path in the user's Dropbox to save the file.
.withMode(WriteMode.OVERWRITE) //always overwrite existing file
.uploadAndFinish(inputStream);
Log.d("Upload Status", "Success");
} catch (DbxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It works correct when 'file' is a directory. But when it is a folder, I will get this exception for the first line:
open failed: EISDIR(is a directory)
Can I fix this problem? Or it must be a directory?
When I run java -jar MidiTest.jar, input a MIDI file, it throws:
Exception in thread "main" java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidi
leReader.java:209)
at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:802)
at MidiTest.playMidi(MidiTest.java:56)
at MidiTest.(MidiTest.java:44)
at MidiTest.main(MidiTest.java:25)
If I use java MidiTest instead it could play without issue. What wrong with the code? I have already add Main-Class: MidiTest with newline on Manifest file
My code:
private void playMidi() {
if(isPlaying.equals("0")) {
try {
song = MidiSystem.getSequence(
getClass().getResource(filename));
sequencer = MidiSystem.getSequencer();
sequencer.setSequence(song);
sequencer.open();
sequencer.addMetaEventListener(this);
sequencer.start();
} catch (InvalidMidiDataException e) {
System.out.println("Bad midi file: "
+ filename);
System.exit(1);
} catch (MidiUnavailableException e) {
System.out.println("No sequencer available");
System.exit(1);
} catch (IOException e) {
System.out.println("Could not read: "
+ filename);
System.exit(1);
}
displayMidiInfo(filename);
} else {
updateTempoFactor(speed);
}
}
You don't appear to be checking if the resource you're trying to get is returning something non-null. Specifically:
song = MidiSystem.getSequence(
getClass().getResource(filename));
is causing this particular problem. There might be a deeper issue, which is that unless the resource represented by filename is actually in the jar, on the Class-Path or in the same directory as the jar file getResource() is not going to find it. If you're trying to access a file anywhere in the general filesystem (not in the jar file) then you should be using File:
song = MidiSystem.getSequence(new File(filename));