「javanio行读取」java nio按行读取文件

博主:adminadmin 2022-12-19 22:48:08 58

本篇文章给大家谈谈javanio行读取,以及java nio按行读取文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java怎样读取文件所有内容,主要是跳行问题?谢谢了

1.nextint()等一系列类似的从控制台取数字的操作,都与一个共性 就是“只取数字”部分。什么意思呢,当控制台提示你输入数字时 比如你

输入:123(回车) ,这实际的字符串是:在windows平台上:123\r\n;在linux平台上是:123\n。而我们的

nextint() 只接受了 数字 123 而 “回车”字符却仍然在缓冲区中,则现在使用nextline()时发现,用户根本没有输入,就执行过去

了这个语句,因为程序自动把上个缓冲中的“回车”字符串内容赋值给了nextline(),恰好 nextline() 又是一“\r\n”作为分界标志

的,所以nextline()中的内容就是一个空字符“”。

2.解决方法:

1).不使用 nextint() ,使用 integer.parseint(scanner.nextline());

2).或者在每个nextint()后多加上一个nextline(),让他来消除掉nextint()中留下的“回车”

3.你的代码可改为:

score = input.nextint();

input.nextline();

scores.add(score);

//或者

score = integer.parseint(input.nextline());

java如何高效读取文本的第N行和最后N行?

一般的需求就用java的io 包里的 BufferedReader缓存一下,效率还算可以,

更高的性能要求,读N行或后N行,可以看看java.nio包里的东西。

下面是一个例子程序,给你参考一下(这里是读第N行的,读后N行的把第一个方法稍作修改即可,不举例了,呵呵):

public class ReadSelectedLine {

static void readLineVarFile(String fileName, int lineNumber) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(

new FileInputStream(fileName)));

String line = reader.readLine();

if (lineNumber 0 || lineNumber getTotalLines(fileName)) {

System.out.println("不在文件的行数范围之内。");

}

int num = 0;

while (line != null) {

if (lineNumber == ++num) {

System.out.println("line " + lineNumber + ": " + line);

}

line = reader.readLine();

}

reader.close();

}

// 文件内容的总行数。

static int getTotalLines(String fileName) throws IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(

new FileInputStream(fileName)));

LineNumberReader reader = new LineNumberReader(in);

String s = reader.readLine();

int lines = 0;

while (s != null) {

lines++;

s = reader.readLine();

}

reader.close();

in.close();

return lines;

}

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

// 读取文件

String fileName = "d:/hst_23.txt";

// 获取文件的内容的总行数

int totalNo = getTotalLines(fileName);

System.out.println("There are "+totalNo+ " lines in the text!");

// 指定读取的行号

int lineNumber = 10;

//读取指定行的内容

readLineVarFile("d:/hst_23.txt", lineNumber);

}

}

介绍一下Java NIO,NIO读取文件都有哪些方法

NIO也就是New I/O,是一组扩展Java IO操作的API集, 于Java 1.4起被引入,Java 7中NIO又提供了一些新的文件系统API,叫NIO2.

NIO2提供两种主要的文件读取方法:

使用buffer和channel类

使用Path 和 File 类

NIO读取文件有以下三种方式:

1. 旧的NIO方式,使用BufferedReader

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class WithoutNIOExample

{

public static void main(String[] args)

{

BufferedReader br = null;

String sCurrentLine = null;

try

{

br = new BufferedReader(

new FileReader("test.txt"));

while ((sCurrentLine = br.readLine()) != null)

{

System.out.println(sCurrentLine);

}

}

catch (IOException e)

{

e.printStackTrace();

}

finally

{

try

{

if (br != null)

br.close();

} catch (IOException ex)

{

ex.printStackTrace();

}

}

}

}

2. 使用buffer读取小文件

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class ReadFileWithFileSizeBuffer

{

public static void main(String args[])

{

try

{

RandomAccessFile aFile = new RandomAccessFile(

"test.txt","r");

FileChannel inChannel = aFile.getChannel();

long fileSize = inChannel.size();

ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);

inChannel.read(buffer);

buffer.rewind();

buffer.flip();

for (int i = 0; i fileSize; i++)

{

System.out.print((char) buffer.get());

}

inChannel.close();

aFile.close();

}

catch (IOException exc)

{

System.out.println(exc);

System.exit(1);

}

}

}

3. 分块读取大文件

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

public class ReadFileWithFixedSizeBuffer

{

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

{

RandomAccessFile aFile = new RandomAccessFile

("test.txt", "r");

FileChannel inChannel = aFile.getChannel();

ByteBuffer buffer = ByteBuffer.allocate(1024);

while(inChannel.read(buffer) 0)

{

buffer.flip();

for (int i = 0; i buffer.limit(); i++)

{

System.out.print((char) buffer.get());

}

buffer.clear(); // do something with the data and clear/compact it.

}

inChannel.close();

aFile.close();

}

}

4. 使用MappedByteBuffer读取文件

import java.io.RandomAccessFile;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

public class ReadFileWithMappedByteBuffer

{

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

{

RandomAccessFile aFile = new RandomAccessFile

("test.txt", "r");

FileChannel inChannel = aFile.getChannel();

MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());

buffer.load();?

for (int i = 0; i buffer.limit(); i++)

{

System.out.print((char) buffer.get());

}

buffer.clear(); // do something with the data and clear/compact it.

inChannel.close();

aFile.close();

}

}

java nio可不可以按行读取

设置两个缓冲区,一大一小,大的缓冲区为每次读取的量,小的缓冲区存放每行的数据(确保大小可存放文本中最长的那行)。

读取的时候判断是不是换行符13,是的话则返回一行数据,不是的话继续读取,直到读完文件。

java nio怎么读取buffer

java NIO中的Buffer的array()方法在能够读和写之前,必须有一个缓冲区,用静态方法 allocate() 来分配缓冲区:

ByteBuffer buffer = ByteBuffer.allocate(1024);

allocate() 方法分配一个具有指定大小的底层数组,并将它包装到一个缓冲区对象中 — 在本例中是一个 ByteBuffer。

还可以将一个现有的数组转换为缓冲区:

byte array[] = new byte[1024];

ByteBuffer buffer = ByteBuffer.wrap(array);

本例使用了 wrap() 方法将一个数组包装为缓冲区。一旦完成包装,底层数据就可以通过缓冲区或者直接访问

关于javanio行读取和java nio按行读取文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-19,除非注明,否则均为首码项目网原创文章,转载请注明出处。