集册 Java实例教程 处理整个文件的内容

处理整个文件的内容

欢马劈雪     最近更新时间:2020-01-02 10:19:05

384
处理整个文件的内容

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.SeekableByteChannel;
/**来自 
 n o w j a v a . c o m - 时代Java**/

import java.nio.charset.Charset;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;


public class Main {


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

    int bufferSize = 8;

    Path path = Paths.get("/home/docs/users.txt");/*from 时代Java公众号*/

    final String newLine = System.getProperty("line.separator");

    try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.WRITE)) {

      ByteBuffer buffer;


      System.out.println("Contents of File");

      sbc.position(0);

      buffer = ByteBuffer.allocate(bufferSize);

      String encoding = System.getProperty("file.encoding");

      int numberOfBytesRead = sbc.read(buffer);

      System.out.println("Number of bytes read: " + numberOfBytesRead);

      while (numberOfBytesRead > 0) {

        buffer.rewind();

        
展开阅读全文