I try to write byte data to directory I use the following code but i get this
Exception in thread "main" java.io.FileNotFoundException: C:\file (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
at com.innvo.domain.App.main(App.java:17)
My code:
public static void main(String[] args) throws IOException {
File dir = new File("C:\\file");
if (dir.isDirectory())
{
String x="new text string";
File serverFile = new File(dir.getAbsolutePath());
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
System.out.println(x.getBytes());
stream.close();
}else {
System.out.println("not");
}
}
serverFile is a directory. FileOutputStream does not accept directories.
You cannot write to a directory like to a file.
Use something like `
File serverFile = new File(dir,"mynewfile.txt");
Related
I try to put some file fileNamePath in zip archive (arguments are D:\text.txt D:\archive.zip):
public static void main(String[] args) throws IOException {
if (args.length==0) return;
String fileNamePath = args[0];
String zipPath = args[1];
FileOutputStream outputStream = new FileOutputStream(zipPath);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
zipOutputStream.putNextEntry(new ZipEntry(fileNamePath));
File file = new File(fileNamePath);
Files.copy(file.toPath(),zipOutputStream);
zipOutputStream.closeEntry();
zipOutputStream.close();
}
Archive is created but i don't see any file in it. Why?
That code is working perfectly:
zip.java
import java.io.*;
import java.nio.file.*;
import java.util.zip.*;
public class zip
{
public static void main(String[] args) throws IOException {
if (args.length==0) return;
String fileNamePath = args[0];
String zipPath = args[1];
FileOutputStream outputStream = new FileOutputStream(zipPath);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
zipOutputStream.putNextEntry(new ZipEntry(fileNamePath));
File file = new File(fileNamePath);
Files.copy(file.toPath(),zipOutputStream);
zipOutputStream.closeEntry();
zipOutputStream.close();
}
}
I have compiled it under Debian 9 Stretch, OpenJDK 8
I have then created a sample txt file:
hello.txt
Hello World
I then compiled it:
javac zip.java
And finally run it:
java zip hello.txt hello.zip
I extract the .zip and open up hello.txt, returning Hello World
May it be that you have no permissions to read/write D:\?
I zipped a directory called dir2 that contains some text files. I want to create a directory in external storage called dir that holds dir2.
Given a zipped dir2, I am trying to extract so it looks like:
external_storage_path/dir/dir2/textfile.txt
createFile(zis, "dir2/textfile.txt");
private void createFile(ZipInputStream zipIn, String filename) {
if(android.os.Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
boolean created = false;
File newFile = new File(Environment.getExternalStorageDirectory(),
"dir/dir2/textfile.txt");
if(!newFile.exists()) {
// create the file and any parent dirs if needed
created = newFile.mkdirs();
}
if(created) {
BufferedOutputStream bos = null;
try {
FileOutputStream fos = new FileOutputStream(newFile.getAbsolutePath());
bos = new BufferedOutputStream(fos);
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
}
An exception is thrown when trying to create the BufferedOutputStream:
java.io.FileNotFoundException:
/storage/emulated/0/dir/dir2/textfile.txt: open failed: EISDIR (Is a
directory)
The textfile is not a directory though.
I'm trying to read a .xlsx file from the project folder using the following code, but it always throws FileNotFoundException. I have attached the project structure where the file is.
public static void main(String[] args) {
try {
String excelFilePath = "‪DataModel.xlsx";
File file = new File(excelFilePath);
FileInputStream fis = new FileInputStream(file);
} catch (Exception ex) {
System.out.print(ex);
}
}
This is how I normally would approach it:
File file = new File( javaApplication2.class.getResource( excelFilePath ).getPath() );
classLoader.getResource() solved the problem
Suppose I have a .jar file that exports a temple.txt into directory ~/output. The code that does this is below. However suppose that this output folder is actually in home/workspace/temp/output. How do I use the File(..) constructor to send temple.txt to some completely different folder, such as home/Desktop/hello/ ?
public class main {
public static void main(String[] args) throws IOException {
String directory = "output";
FileWriter fstream;
BufferedWriter out;
File file = new File(directory, "temple.txt");
fstream = new FileWriter(file);
out = new BufferedWriter(fstream);
out.write(String.valueOf(1));
out.close();
}
}
I would like to download the temp files from an web server
The code i tried is below, but i am getting only the HTML content being written the ouput file
But the url path contains 712 files in .gz format
This is the code I am using:
import java.io.*;
public class SampleFile{
public static void main(String args[]) throws IOException{
BufferedInputStream in = new BufferedInputStream(new java.net.URL("http://xxx:9080/xxx/xxx/xxx/xxx/xxx.jsp?file=/apps/WasApps/xxx/templogs/xxx.log.xxx_Server1.2012-04-01.gz").openStream());
FileOutputStream fos = new FileOutputStream("LocalPath\\koushik.txt");
BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
int x=0;
byte[] data = new byte[1024];
while((x=in.read(data,0,1024))>=0) {
bout.write(data,0,x);
}
bout.close();
in.close();
}
}