Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要的文件是1529字节,它不工作_Java_File - Fatal编程技术网

Java 我需要的文件是1529字节,它不工作

Java 我需要的文件是1529字节,它不工作,java,file,Java,File,我错过什么了吗?我需要它是1529字节。这是为了我的项目,我不明白发生了什么 我有一个字节数组,我想在指定的文件中一次随机写入一个字节数组 import java.io.*; import java.util.Random; public class Cover { public static byte[] aPenman = {97, 98, 99, 100, 101, 102}; public static long mTabulable(String fileName) throws

我错过什么了吗?我需要它是1529字节。这是为了我的项目,我不明白发生了什么

我有一个字节数组,我想在指定的文件中一次随机写入一个字节数组

import java.io.*;
import java.util.Random;

public class Cover {

public static byte[] aPenman = {97, 98, 99, 100, 101, 102};
public static long mTabulable(String fileName) throws IOException {

    String path = "C:\\Users\\KOSTAS\\IdeaProjects\\prog\\src\\"+fileName;
    File file = new File(path);
    BufferedOutputStream out = null;
    try {
        out = new BufferedOutputStream(new FileOutputStream(file));
    } catch (FileNotFoundException e) {
        System.err.println("Unable to open file " + fileName);
    }

    long sum = 0;
    try {
        int i = 0;
        while (file.length() != 1529){
            int rand = new Random().nextInt(aPenman.length);
            out.write(aPenman[rand]);
            if (i < 1347) {
                sum += aPenman[rand];
            }
            i++;
        }
        out.close();
    }catch (Exception e){
        System.err.println("Unable to open file " + fileName);
        }
        return sum;
    }

}
import java.io.*;
导入java.util.Random;
公共类封面{
公共静态字节[]aPenman={97,98,99,100,101,102};
公共静态长mTabulable(字符串文件名)引发IOException{
String path=“C:\\Users\\KOSTAS\\IdeaProjects\\prog\\src\\”+文件名;
文件=新文件(路径);
BufferedOutputStream out=null;
试一试{
out=新的BufferedOutputStream(新文件输出流(文件));
}catch(filenotfounde异常){
System.err.println(“无法打开文件”+文件名);
}
长和=0;
试一试{
int i=0;
while(file.length()!=1529){
int rand=new Random().nextInt(aPenman.length);
写出(阿彭曼[兰德]);
如果(i<1347){
sum+=aPenman[rand];
}
i++;
}
out.close();
}捕获(例外e){
System.err.println(“无法打开文件”+文件名);
}
回报金额;
}
}

我认为您缺少一个:

out.flush();
就在你的下面:

out.write(aPenman[rand]);
在while循环中


这将强制将任何缓冲输出字节写入底层输出流。

我认为您缺少一个:

out.flush();
就在你的下面:

out.write(aPenman[rand]);
在while循环中


这将强制将任何缓冲输出字节写入底层输出流。

如果每次通过循环打印文件长度,它会是什么样子?你能猜出发生了什么事吗?您为for
out
创建实例的类的名称是否为您提供了线索?如果您每次通过循环打印文件的长度,它是什么样子的?你能猜出发生了什么事吗?创建for
out
实例的类的名称是否为您提供了线索?虽然这解决了问题,但最好跟踪写入的字节数,而不是重复检查
file.length()
,因为OP现在正在写入和刷新单个字节,这使得使用
BufferedOutputStream
无用。虽然这解决了问题,但最好跟踪写入的字节数,而不是重复检查
file.length()
,因为现在OP正在写入和刷新单个字节,这使得使用
BufferedOutputStream
无用。