javaはtxtテキストツール類を読み、行ごとにtxtを読んでListセットに戻り、すべてを読んでSteringに戻ります。
21136 ワード
一、行ごとにtxtを読み、List集合に戻る
まずtxtテキストをファイルのバイトストリームFileInputStreamとして読み、文字ストリームに変換し、文字ストリームバッファ領域を作成して文字ストリームを読み出し、文字バッファ領域BufferdReaderのreadLine()方法により、行ごとに読み出す。キーコード:
バイトストリーム読み出しまたは文字ストリーム読み出しは、2つの方法で実施されてもよい。バイトストリーム読み出しの場合は、まず1バイトの配列content[]を定義し、次にfileInput Streamのread方法を使用して、バイトとして読み出し、定義されたバイト配列に入れて、最終的にはこのようなバイト配列に戻ることができる。キーコード:
まずtxtテキストをファイルのバイトストリームFileInputStreamとして読み、文字ストリームに変換し、文字ストリームバッファ領域を作成して文字ストリームを読み出し、文字バッファ領域BufferdReaderのreadLine()方法により、行ごとに読み出す。キーコード:
File file = new File("F:\\IDEA project\\test.txt");
//
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),
"utf-8");
//
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String string = null;
while ((string = bufferedReader.readLine()) != null) {
System.out.println(string);
}
txtUtilツール類の方法 /**
* txt List
* @param path
* @return List
*/
public static List<String> getFileContent(String path) {
List<String> strList = new ArrayList<>();
File file = new File(path);
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
//
inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");
//
bufferedReader = new BufferedReader(inputStreamReader);
String line;
//
while ((line = bufferedReader.readLine()) != null) {
strList.add(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return strList;
}
二.txtテキストのすべての内容を一度に読んで、Steringに戻ります。バイトストリーム読み出しまたは文字ストリーム読み出しは、2つの方法で実施されてもよい。バイトストリーム読み出しの場合は、まず1バイトの配列content[]を定義し、次にfileInput Streamのread方法を使用して、バイトとして読み出し、定義されたバイト配列に入れて、最終的にはこのようなバイト配列に戻ることができる。キーコード:
byte[] fileContent = new byte[1024];
FileInputStream fileInputStream = new FileInputStream(new File("FilePath"));
fileInputStram.read(fileContent);
コピー&ペーストが利用可能なツールクラスの方法: /**
* txt
* @param filePath
* @return String
*/
public static String getAllTxt(String filePath) {
File file = new File(filePath);
Long fileLengthLong = file.length();
byte[] fileContent = new byte[fileLengthLong.intValue()];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(fileContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(fileContent);
}
すべてのtxtテキストを文字ストリーム読み出しで読み出して一つのSteringに戻すこともできます。 /**
* String
* @param filePath
* @return String
*/
public String getAllTxtByChar(String filePath) {
File file = new File(filePath);
Long fileLength = file.length();
char[] fileContent = new char[fileLength.intValue()];
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(new FileInputStream(file));
inputStreamReader.read(fileContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(fileContent);
}