有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试制作一个程序,它将把文件作为输入,并将其保存到某个位置,然后将其复制到另一个位置。但不幸的是,我得到的输出文件已损坏。我做错了什么

文件类:

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) 个答案

  1. # 1 楼答案

    首先必须说远离Vector。只需谷歌“JavaVectorBad”或类似内容

    我不知道你为什么要保留字节数组列表。我看不出你在哪里用你读到的单个byte[]填充列表

    冒着因没有回答你的问题而被否决的风险,我只是离开了你的文本

    I am trying to make a program which will take the file as input, and save it to some copy it to some other location.

    我不明白为什么不是这样

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