Java 文件內(nèi)容

2018-02-05 15:34 更新

Java IO教程 - Java文件內(nèi)容

文件的內(nèi)容類型

Files.probeContentType(Path path)方法探測文件的內(nèi)容類型。

該方法以多用途Internet郵件擴展(MIME)內(nèi)容類型的值的字符串形式返回內(nèi)容類型。

如果一個文件的內(nèi)容類型不能確定,則返回null。

以下代碼顯示如何探測文件的內(nèi)容類型。

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    Path p = Paths.get("C:\\Java_Dev\\test1.txt");

    try {
      String contentType = Files.probeContentType(p);
      System.out.format("Content type   of  %s  is %s%n", p, contentType);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

上面的代碼生成以下結(jié)果。


讀取文件的內(nèi)容

Files類包含以下方法來讀取文件的內(nèi)容作為字節(jié)和文本行:

  • static byte[] readAllBytes(Path path) - 讀取文件中的所有字節(jié)。
  • static List readAllLines(Path path) - 讀取文本文本行的整個內(nèi)容。
  • static List readAllLines(Path path, Charset cs)

Files類可以從Path對象獲取InputStream和BufferedReader對象。

newInputStream(Path path,OpenOption ... options)方法返回指定路徑的InputStream對象。它假定文件的內(nèi)容是UTF-8字符集。

newBufferedReader(Path path)和newBufferedReader(Path path,Charset cs)方法返回一個BufferedReader。我們可以指定字符集。

Files類提供了使用其newByteChannel(Path path,OpenOption ... options)方法從Path對象中獲取SeekableByteChannel對象的方法。

OpenOption類型配置正在打開的文件。下表列出了OpenOption類型的值及其描述。OpenOption是java.nio.file包中的一個接口。

java.nio.file包中的StandardOpenOption枚舉實現(xiàn)了OpenOption接口。

標準打開選項描述
APPEND將寫入的數(shù)據(jù)附加到現(xiàn)有文件,如果文件被打開寫入。
CREATE創(chuàng)建一個新文件,如果它不存在。
CREATE_NEW創(chuàng)建一個新文件,如果它不存在。如果文件已存在,則操作失敗。
DELETE_ON_CLOSE關(guān)閉流時刪除文件。 在與臨時文件一起使用時非常有用。
DSYNC保持文件的內(nèi)容與底層存儲同步。
READ打開具有讀訪問權(quán)限的文件。
SPARSE如果它與CREATE_NEW選項一起使用,它對文件系統(tǒng)提示新文件應(yīng)該是稀疏文件。
SYNC保持文件的內(nèi)容和元數(shù)據(jù)與底層存儲同步。
TRUNCATE_EXISTING如果打開文件以進行寫訪問,則將現(xiàn)有文件的長度截斷為零。
WRITE打開文件以進行寫訪問。

以下代碼在默認目錄中為test2.txt文件獲取一個SeekableByteChannel對象。

它打開文件以進行讀取和寫入訪問。它使用CREATE選項,因此如果文件不存在,則創(chuàng)建該文件。

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE;

import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) throws Exception {
    Path src = Paths.get("test2.txt");
    SeekableByteChannel sbc = Files.newByteChannel(src, READ, WRITE, CREATE);
  }
}

以下代碼演示了如何讀取和顯示我們默認目錄中test1.txt文件的內(nèi)容。 如果文件不存在,程序?qū)@示一條錯誤消息。

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class Main {
  public static void main(String[] args) throws Exception{
    Charset cs = Charset.forName("US-ASCII");
    Path source = Paths.get("test1.txt");

    List<String> lines = Files.readAllLines(source, cs);
    for (String line : lines) {
        System.out.println(line);
    }
  }
}

寫入文件

我們可以使用Files類的以下write()方法將內(nèi)容寫入文件。

static Path  write(Path path, byte[]  bytes,  OpenOption... options)
static Path  write(Path path, Iterable lines, OpenOption... options)
static Path  write(Path path, Iterable lines, Charset cs, OpenOption... options)

write()方法打開文件,將傳遞的內(nèi)容寫入文件,并關(guān)閉它。

如果沒有打開選項,它將使用CREATE,TRUNCATE_EXISTING和WRITE選項打開文件。

如果我們正在向文件寫入文本,它會寫一個平臺相關(guān)的行分隔符。

如果在寫入文本行時未指定charset,則假定使用UTF-8字符集。

以下代碼演示如何使用write()方法將文本行寫入文件。

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<String> texts = new ArrayList<>();
    texts.add("test");
    texts.add("test");
    Path dest = Paths.get("twinkle.txt");
    Charset cs = Charset.forName("US-ASCII");
    try {
      Path p = Files.write(dest, texts, cs, WRITE, CREATE);
      System.out.println("Text was written to " + p.toAbsolutePath());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Files.newOutputStream(Path path,OpenOption ... options)返回指定路徑的OutputStream。

Files.newBufferedWriter(Path path,Charset cs,OpenOption ...選項)方法為指定的路徑返回BufferedWriter。

上面的代碼生成以下結(jié)果。

隨機訪問文件

SeekableByteChannel對象提供對文件的隨機訪問。

我們可以使用Files類的newByteChannel()方法為Path獲取一個SeekableByteChannel對象,如下所示:

Path  src = Paths.get("test.txt"); 
SeekableByteChannel seekableChannel  = Files.newByteChannel(src, READ,  WRITE,  CREATE,  TRUNCATE_EXISTING);

我們可以使用size()方法以字節(jié)為單位獲取SeekableByteChannel實體的大小。

由于數(shù)據(jù)被截斷或?qū)懭胪ǖ?,因此更新了大小?/p>

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path src = Paths.get("test.txt");
    String encoding = System.getProperty("file.encoding");
    Charset cs = Charset.forName(encoding);
    try (SeekableByteChannel seekableChannel = Files.newByteChannel(src, READ,
        WRITE, CREATE, TRUNCATE_EXISTING)) {
      printDetails(seekableChannel, "Before writing data");
      writeData(seekableChannel, cs);
      printDetails(seekableChannel, "After writing data");

      seekableChannel.position(0);
      printDetails(seekableChannel, "After resetting position to 0");
      readData(seekableChannel, cs);
      printDetails(seekableChannel, "After reading data");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void writeData(SeekableByteChannel seekableChannel, Charset cs)
      throws IOException {
    String separator = System.getProperty("line.separator");
    StringBuilder sb = new StringBuilder();
    sb.append("test");
    sb.append(separator);
    sb.append("test2");
    sb.append(separator);

    CharBuffer charBuffer = CharBuffer.wrap(sb);
    ByteBuffer byteBuffer = cs.encode(charBuffer);
    seekableChannel.write(byteBuffer);
  }

  public static void readData(SeekableByteChannel seekableChannel, Charset cs)
      throws IOException {
    ByteBuffer byteBuffer = ByteBuffer.allocate(128);
    String encoding = System.getProperty("file.encoding");
    while (seekableChannel.read(byteBuffer) > 0) {
      byteBuffer.rewind();
      CharBuffer charBuffer = cs.decode(byteBuffer);
      System.out.print(charBuffer);
      byteBuffer.flip();
    }
  }

  public static void printDetails(SeekableByteChannel seekableChannel,
      String msg) {
    try {
      System.out.println(msg + ": Size   = " + seekableChannel.size()
          + ", Position = " + seekableChannel.position());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號