「Java」FileInputStreamでファイルからバイトずつ読み込むサンプル

環境
Java 1.8
Eclipse 2019

ファイル名: C:\study\test.txt 内容: ABCD

書式
1.public FileInputStream(String name) throws FileNotFoundException

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ファイル・システム内のパス名nameで指定される実際のファイルへの接続を開くことにより、FileInputStreamを作成します。
このファイル接続を表すために、新しいFileDescriptorオブジェクトが作成されます。
ファイル・システム内のパス名nameで指定される実際のファイルへの接続を開くことにより、FileInputStreamを作成します。 このファイル接続を表すために、新しいFileDescriptorオブジェクトが作成されます。
ファイル・システム内のパス名nameで指定される実際のファイルへの接続を開くことにより、FileInputStreamを作成します。
このファイル接続を表すために、新しいFileDescriptorオブジェクトが作成されます。

2.public InputStreamReader(InputStream in,String charsetName) throws UnsupportedEncodingException
指定された文字セットを使うInputStreamReaderを作成します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.testinfo;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamDemo {
//ファイルパスの宣言
private static final String FILEPATH = "c:\\study\\";
//エンコードの宣言
private static final String CHARSET = "UTF-8";
@SuppressWarnings("resource")
public static void main(String[] args) {
try {
// FileInputStreamを使用し、ファイルの内容を1バイトずつ読み込む
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(FILEPATH + "test.txt"),
CHARSET);
int resByte;
System.out.println("文字コード");
while ((resByte = inputStreamReader.read()) != -1) {
System.out.println(resByte);
}
} catch (Exception ex) {
}
}
}
package com.arkgame.testinfo; import java.io.FileInputStream; import java.io.InputStreamReader; public class InputStreamDemo { //ファイルパスの宣言 private static final String FILEPATH = "c:\\study\\"; //エンコードの宣言 private static final String CHARSET = "UTF-8"; @SuppressWarnings("resource") public static void main(String[] args) { try { // FileInputStreamを使用し、ファイルの内容を1バイトずつ読み込む InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(FILEPATH + "test.txt"), CHARSET); int resByte; System.out.println("文字コード"); while ((resByte = inputStreamReader.read()) != -1) { System.out.println(resByte); } } catch (Exception ex) { } } }
package com.arkgame.testinfo;

import java.io.FileInputStream;
import java.io.InputStreamReader;

public class InputStreamDemo {

      //ファイルパスの宣言
      private static final String FILEPATH = "c:\\study\\";
      //エンコードの宣言
      private static final String CHARSET = "UTF-8";

      @SuppressWarnings("resource")
      public static void main(String[] args) {
            try {
                  // FileInputStreamを使用し、ファイルの内容を1バイトずつ読み込む
                  InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(FILEPATH + "test.txt"),
                              CHARSET);
                  int resByte;
                  System.out.println("文字コード");
                  while ((resByte = inputStreamReader.read()) != -1) {
                        System.out.println(resByte);
                  }
            } catch (Exception ex) {
            }

      }

}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
文字コード
65
66
67
68
文字コード 65 66 67 68
文字コード
65
66
67
68

 

Java

Posted by arkgame