Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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 从文件中读取64个字符的集合_Java_Arrays_Bufferedreader_Filereader - Fatal编程技术网

Java 从文件中读取64个字符的集合

Java 从文件中读取64个字符的集合,java,arrays,bufferedreader,filereader,Java,Arrays,Bufferedreader,Filereader,所以我试图从一个文件中读入一个字符串。但是,如果最后一个字符串中没有64个字符,我希望每个字符串只包含64个字符或更少。所以本质上我有一个计数器,当计数器达到64时,我将字符数组设置为字符串,转到下一行并将计数重置为零。然而,当我运行它时,我没有得到任何类型的输出。感谢您的帮助。下面是我的代码片段 public static void main(String[] args) throws IOException{ File input = null; if (1 < ar

所以我试图从一个文件中读入一个字符串。但是,如果最后一个字符串中没有64个字符,我希望每个字符串只包含64个字符或更少。所以本质上我有一个计数器,当计数器达到64时,我将字符数组设置为字符串,转到下一行并将计数重置为零。然而,当我运行它时,我没有得到任何类型的输出。感谢您的帮助。下面是我的代码片段

public static void main(String[] args) throws IOException{

    File input = null;
    if (1 < args.length) {
        input = new File(args[1]);
    }     
    else {
        System.err.println("Invalid arguments count:" + args.length);
        System.exit(0);
    }
    String key = args[0];
    BufferedReader reader = null;
    int len;
    Scanner scan = new Scanner(System.in);
    System.out.println("How many lines in the file?");
    if(scan.hasNextInt()){
        len = scan.nextInt();
    }
    else{
        System.out.println("Please enter an integer: ");
        scan.next();
        len = scan.nextInt();
    }
    scan.close();
    String[] inputText = new String[2 * len];
    String[] encryptText = new String[2 * len];
    char[][] inputCharArr = new char[2 * len][64];
    reader = new BufferedReader(new FileReader(input));
    int r;
    int counter = 0;
    int row = 0;
    while ((r = reader.read()) != -1) {
        char ch = (char) r;
        if(counter == 64){
            String temp = new String(inputCharArr[row]);
            inputText[row] = temp;
            encryptText[row] = inputText[row];
            System.out.println(inputText[row]);
            row++;
            counter = 0;
        }
        if(row == len){
            break;
        }
        inputCharArr[row][counter] = ch;
        counter++;
    }

要从文件中读取64个字符,请创建一个
char[64]
缓冲区并调用。比调用
read()
64次要快得多(也容易得多)。你能给我举个小例子说明我如何使用它吗?啊,好的,我现在知道了,创建一个大小为64的字符数组并读入其中。谢谢你的帮助!要从文件中读取64个字符,请创建一个
char[64]
缓冲区并调用。比调用
read()
64次要快得多(也容易得多)。你能给我举个小例子说明我如何使用它吗?啊,好的,我现在知道了,创建一个大小为64的字符数组并读入其中。谢谢你的帮助!
        CharBuffer cbuf = CharBuffer.allocate(64);
    int counter = 0;
    while (reader.read(cbuf) != -1) {
        inputText[counter] = cbuf.toString();
        encryptText[counter] = inputText[counter];
        counter++;
        cbuf.clear();
    }