我正在尝试制作一个程序,将文件作为输入,并将其保存到一些复制到其他位置。但不幸的是,我得到的输出文件是损坏的。我做错了什么?
文件类:
package server.client;
import java.util.Vector;
public class File {
public byte [] inArray;
public Vector <byte [] > inVector;
public byte [] outArray;
public Vector <byte [] > outVector;
public String name;
public String savePath;
public File(String name)
{
outVector=new Vector<byte []>();
this.name=name;
}
public File(byte [] array,String name){
this.inArray=array;
this.name=name;
System.out.println("Input has "+inArray.length+" bytes");
int vectorSize=inArray.length /1024 + 1;
System.out.println("vector size: "+ vectorSize);
inVector= new Vector<byte []>(vectorSize);
int c=0;
for(int i=0; i<vectorSize;i++){
if(vectorSize==1){
byte [] temp=new byte[inArray.length];
for(int j=0;i< inArray.length;i++){
temp[j]=inArray[j];
}
inVector.add(temp);
System.out.println("Input has 1 byte vector");
}
else{
System.out.println("Input has multiple byte vectors");
byte [] temp=new byte[1024];
for(int j=0;j< 1024; j++){
if(c==array.length)
break;
temp[j]=array[c];
c++;
}
inVector.add(temp);
}
}
}
public void addToVector(byte [] ar){
outVector.add(ar);
}
public void prepareOutArray(){
int c=0;
for(int i=0;i<outVector.size(); i++){
byte[] temp=outVector.elementAt(i);
for(int j=0;j<temp.length;j++){
c++;
}
}
System.out.println("File has "+c +" bytes");
outArray=new byte[c];
int f=0;
for(int i=0;i<outVector.size(); i++){
byte[] temp=outVector.elementAt(i);
for(int j=0;j<temp.length;j++){
if(f==1024*i + j)
break;
outArray[f]+=temp[j];
f++;
}
}
}
}
主类:
package server.client;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class ServerClient {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("File name (including format:");
String nm=input.nextLine();
System.out.println("File location:");
String path=input.nextLine();
Path myPath=Paths.get(path);
byte [] array=null;
try {
array=Files.readAllBytes(myPath);
} catch (IOException ex) {
Logger.getLogger(ServerClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("couldnt read the file");
}
File fileObject=new File(array,nm);
System.out.println("copying item");
File outputObject=new File("whattoDO.txt");
for(int i=0;i< fileObject.inVector.size() ;i++){
outputObject.addToVector(fileObject.inVector.elementAt(i));
}
System.out.println("C:/Users/Ejub/Documents/"+outputObject.name);
FileOutputStream fileOut=null;
outputObject.prepareOutArray();
System.out.println("");
try {
fileOut=new FileOutputStream("C:/Users/Ejub/Documents/"+outputObject.name);
fileOut.write(outputObject.outArray);
} catch (FileNotFoundException ex) {
Logger.getLogger(ServerClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("couldnt write to file1");
} catch (IOException ex) {
Logger.getLogger(ServerClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("couldnt write to file2");
}
必须首先说远离Vector
。只要谷歌"java Vector bad"或类似的。
我不知道你为什么要保留一个字节数组列表。我不知道你在哪里用你读到的单个byte[]
填充列表。
冒着不真的回答你的问题的风险,我只是离开你的文本
我正在尝试编写一个程序,它将文件作为输入,并将其保存到某些文件,并将其复制到其他位置。
我不明白为什么它不是像
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("File name to move?");
String name = scanner.next();
System.out.println("Destination?");
String destination = scanner.next();
File current = new File(name);
File movedFile = new File(destination);
try {
byte[] content = Files.readAllBytes(current.toPath());
Files.write(movedFile.toPath(),content, StandardOpenOption.CREATE);
} catch (IOException e) {
throw new IllegalStateException("Failed to read from " +
current.getAbsolutePath() + ", or write to " + movedFile.getAbsolutePath(),e);
}
}