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:\?
Related
I made a Program which moves all files of a folder to another folder but when I'm executing the Program it shows me java.io.FileNotFound Exception and Access is denied
I tried to fix this error but it is still giving me the same error.
What am I doing wrong?
I have also attached the screenshot of Error I am getting.
when I checked the properties of the folder it was read-only, I changed but still, the Issue Persists
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MoveFile
{
public static void main(String\[\] args)
{
InputStream inStream = null;
OutputStream outStream = null;
try{
File afile =new File("C:\\Users\\admin\\Desktop\\A");
File bfile =new File("C:\\Users\\admin\\Desktop\\B");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte\[\] buffer = new byte\[1024\];
int length;
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
afile.delete();
System.out.println("File Moved!");
}catch(IOException e){
e.printStackTrace();
}
}
}
I expect the Files of folder A to move in Folder B.
according to your question description , you try to move all file of a folder to another folder...
to do this you can use this code ....
public static void main(String[] args) throws IOException {
String from = "C:/Users/Infra/Desktop/PBL/A";
String to = "C:/Users/Infra/Desktop/PBL/B";
File folder = new File(from);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
Path temp = Files.move(Paths.get(from +"/"+ file.getName()),
Paths.get(to +"/"+ file.getName()));
}
}
I want to make a win zip software. I am reading java stream classes and zip class but my mind can't work in this topic. Please help me How to do this. This my minor project in my college. I made two project in java.
Library management System
Diagnostic Lab Management
But Teacher say Management project not allowed. Please help me
This code work in one directory make for multidirectory
My Program Code
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.*;
import java.io.*;
class MyZip{
FileInputStream fis;
BufferedInputStream bis;
FileOutputStream fos;
BufferedOutputStream bos;
ZipOutputStream zout;
ZipEntry ze;
public MyZip()throws IOException{
Console con=System.console();
System.out.println("How many directories do u want to compressed:");
int no_file=Integer.parseInt(con.readLine());
String inputFile[]=new String[no_file];
File fileArray[]=new File[no_file];
System.out.println("Enter the path of directories to be compressed:");
for(int i=0;i<inputFile.length;i++){
inputFile[i]=con.readLine();
fileArray[i]=new File(inputFile[i]);
}
String outputFile="E:\\MyProgram\\MyZip\\MyZip.zip";
fos=new FileOutputStream(outputFile);
bos=new BufferedOutputStream(fos);
zout=new ZipOutputStream(bos);
zipFile(fileArray);
zout.close();
bos.close();
fos.close();
getZipEntry();
}
public void getZipEntry()throws IOException{
ZipFile zf=new ZipFile("E:\\MyProgram\\MyZip\\MyZip.zip");
System.out.println(zf.getName());//return name of zip file
Enumeration e=zf.entries();
while(e.hasMoreElements()){
ZipEntry ze=(ZipEntry)e.nextElement();
System.out.print(ze.getName()+"\t");//return name of entry
System.out.print(ze.getSize()+"\t");//return uncompressed size
System.out.print(ze.getCompressedSize());//return compressed size
System.out.println();
}
zf.close();
}
public void zipFile(File farr[])throws IOException{
for(File f:farr){
if(f.isFile()){
writeFile(f);
}
if(f.isDirectory()){
File fileArray[]=f.listFiles();
zipFile(fileArray);
}
}
}
public void writeFile(File f)throws IOException{
ze=new ZipEntry(f.getPath());
zout.putNextEntry(ze);
fis=new FileInputStream(f);
bis=new BufferedInputStream(fis);
int ch;
while((ch=bis.read())!=-1)
zout.write(ch);
bis.close();
fis.close();
zout.closeEntry();
zout.flush();
}
public static void main(String args[]) throws IOException{
new MyZip();
}
}
Use for unzip your code only for zip
import java.io.*;
import java.util.*;
import java.util.zip.*;
class MyUnZip
{
FileInputStream fis;
BufferedInputStream bis;
ZipInputStream zis;
FileOutputStream fos;
BufferedOutputStream bos;
public MyUnZip(String inputFile)throws IOException{
String path1=inputFile.substring(0,inputFile.lastIndexOf('.'));
//System.out.println(path1);
fis=new FileInputStream(inputFile);
bis=new BufferedInputStream(fis);
zis=new ZipInputStream(bis);
File outputFile;
ZipEntry ze=null;
while((ze=zis.getNextEntry())!=null){
String str=ze.getName();
//System.out.println(str);
File fileName=new File(str);
String path2=str.substring(str.indexOf('\\'),str.lastIndexOf('\\'));
//System.out.println(path2);
String path3=path1+path2;
System.out.println(path3);
File filePath=new File(path3);
filePath.mkdirs();
outputFile=new File(path3,fileName.getName());
fos=new FileOutputStream(outputFile);
bos=new BufferedOutputStream(fos);
int ch;
while((ch=zis.read())!=-1)
bos.write(ch);
bos.close();
fos.close();
zis.closeEntry();
}
zis.close();
}
public static void main(String args[]) throws IOException{
Console con=System.console();
System.out.println("Enter the path of zip file to be uncompressed:");
String inputFile=con.readLine();
new MyUnZip(inputFile);
}
}
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");
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();
}
}