Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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:如何使用FileChannel从文件中读取数字_Java - Fatal编程技术网

Java:如何使用FileChannel从文件中读取数字

Java:如何使用FileChannel从文件中读取数字,java,Java,我有一个很大的文本文件,其中有数千个数字,这些数字之间用空格隔开。我想用Java FileChannel读取每个数字 我能够使用这里提到的第一种方法读取文件:但我不确定如何读取两位、三位和四位数字的每个数字 我的代码: public static void main(String args[]) { try { String file_name="abc.txt"; RandomAccessFile input_file = new Random

我有一个很大的文本文件,其中有数千个数字,这些数字之间用空格隔开。我想用Java FileChannel读取每个数字

我能够使用这里提到的第一种方法读取文件:但我不确定如何读取两位、三位和四位数字的每个数字

我的代码:

public static void main(String args[]) {

    try
    {
        String file_name="abc.txt";
        RandomAccessFile input_file = new RandomAccessFile(file_name,"r");
        FileInputStream in = new FileInputStream(file_name);
        FileChannel ch = in.getChannel();
        FileChannel inChannel = input_file.getChannel();
        long file_size = inChannel.size();
        ByteBuffer buffer = ByteBuffer.allocate((int) file_size);

        Charset cs = Charset.forName("ASCII");
        // inChannel.read(buffer);

        int rd;
        while ( (rd = ch.read( buffer )) != -1 ) 
        {
            buffer.rewind();
            System.out.println("String read: ");
            CharBuffer chbuf = cs.decode(buffer);

            for ( int i = 0; i < chbuf.length(); i++ ) 
            {

               System.out.print(chbuf.get());
            }
            buffer.clear();
            inChannel.close();
            input_file.close();
        }
    }
    catch (IOException exc){}
publicstaticvoidmain(字符串参数[]){
尝试
{
字符串文件_name=“abc.txt”;
RandomAccessFile输入文件=新的RandomAccessFile(文件名,“r”);
FileInputStream in=新的FileInputStream(文件名);
FileChannel ch=in.getChannel();
FileChannel inChannel=input_file.getChannel();
长文件_size=inChannel.size();
ByteBuffer buffer=ByteBuffer.allocate((int)文件大小);
Charset cs=Charset.forName(“ASCII”);
//inChannel.read(缓冲区);
国际公路;
而((rd=ch.read(缓冲区))!=-1)
{
buffer.rewind();
System.out.println(“字符串读取:”;
CharBuffer chbuf=cs.decode(缓冲区);
对于(int i=0;i
}
}

我已经修改了您的代码,将每个数字分开(不需要太多修改):

import java.io.FileInputStream;
导入java.io.IOException;
导入java.io.RandomAccessFile;
导入java.nio.ByteBuffer;
导入java.nio.CharBuffer;
导入java.nio.IntBuffer;
导入java.nio.channels.FileChannel;
导入java.nio.charset.charset;
导入java.util.ArrayList;
公共类读取文件{
公共静态void main(字符串参数[]){
尝试
{   
字符串s;
整数I;
字符串文件_name=“C:/Users/User/test.txt”;
RandomAccessFile输入文件=新的RandomAccessFile(文件名,“r”);
FileInputStream in=新的FileInputStream(文件名);
FileChannel ch=in.getChannel();
FileChannel inChannel=input_file.getChannel();
长文件_size=inChannel.size();
ByteBuffer buffer=ByteBuffer.allocate((int)文件大小);
Charset cs=Charset.forName(“ASCII”);
ArrayList字符=新的ArrayList();
//inChannel.read(缓冲区);
国际公路;
而((rd=ch.read(缓冲区))!=-1)
{
buffer.rewind();
CharBuffer chbuf=cs.decode(缓冲区);
System.out.println(“ASCII值读取:”);
System.out.println();
对于(int i=0;i
你是说第一个使用nio的方法吗?我甚至不能让它工作。我得到了一个java.nio.BufferUnderflowException。我的意思是1)在文件大小的缓冲区中读取一个小文件,它工作得很好…您在任何时候都得到了一个java.nio.BufferUnderflowException吗?在这种情况下,您读取的文件是什么样子的?你需要修改代码吗?哦,对不起,我忘了提到我做了一些更改,然后它开始工作,但无论如何,现在正在读取文件。问题是我想分别阅读每个数字。添加,请查看说明。
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;

public class ReadFile {

    public static void main(String args[]) {

        try
        {   
            String s; 
            Integer I;
            String file_name="C:/Users/User/test.txt";
            RandomAccessFile input_file = new RandomAccessFile(file_name,"r");
            FileInputStream in = new FileInputStream(file_name);
            FileChannel ch = in.getChannel();
            FileChannel inChannel = input_file.getChannel();
            long file_size = inChannel.size();
            ByteBuffer buffer = ByteBuffer.allocate((int) file_size);

            Charset cs = Charset.forName("ASCII");
            ArrayList<Character> character = new ArrayList<Character>();
            // inChannel.read(buffer);

            int rd;
            while ( (rd = ch.read( buffer )) != -1 ) 
            {
                buffer.rewind();

                CharBuffer chbuf = cs.decode(buffer);

                System.out.println("ASCII values read: ");

                System.out.println();

                     for ( int i = 0; i < chbuf.length(); i++ ) 
                    {

                        int j = chbuf.get();

                        character.add((char)j);

                        System.out.println("j("+i+"): "+j+" ");

                    }

                     System.out.println();

                     System.out.println("Chars they represent: ");

                     System.out.println();

                     for ( int i = 0; i < character.size(); i++ ) 
                        {

                        System.out.println("character("+i+"): "+character.get(i)+" ");
                        System.out.println();
                        s = character.get(i).toString();
                        if(!(s.equals(" ")||s.equals("\r")))
                            {
                            System.out.println("s("+i+"): "+s+" ");
                            System.out.println();
                            System.out.println("s("+i+").length(): "+s.length()+" ");
                            System.out.println();
                            I = new Integer(s);
                            System.out.println("I("+i+"): "+I+" ");
                            System.out.println();
                            }
                        }

                buffer.clear();
                inChannel.close();
                input_file.close();
            }
        }
        catch (IOException exc){}
    }
}