Java程序不使用字节数组正确复制文件

时间:2015-06-08 18:36:34

标签: java file bytearray

我正在尝试创建一个将文件作为输入的程序,并将其保存到一些复制到其他位置。但不幸的是,我得到的输出文件已损坏。我做错了什么?

文件类:

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");
    }

1 个答案:

答案 0 :(得分:1)

首先必须远离Vector。只是google&#34; java Vector bad&#34;或类似的。

我不确定为什么要保留字节数组列表。而且,我不会看到您使用您阅读的单个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);
    }        
}