FileInputStreamクラスとFileOutputStreamクラス
FileInputStreamクラスとFileOutputStreamクラスは、ディスクファイルを操作するために使用されます。FileInputStream(ファイルバイト入力ストリーム)、FileOutputStream(ファイルバイト出力ストリーム)。
FileInputStreamクラスとFileOutputStreamクラスは、ファイルの読み取りおよび書き込み関数を実装できます。
最初にファイルオブジェクトを作成します
public class Study2 {
public static void main(String[] args) {
//创建文件对象
File f = new File("word.txt");
}
}
ファイルに情報を書き込むFileOutputStreamオブジェクトを作成します
public class Study2 {
public static void main(String[] args) {
// 创建文件对象
File f = new File("word.txt");
// 将信息写入到文件中
FileOutputStream out = null;
try {
out = new FileOutputStream(f);// 创建FileOutputStream对象
byte b[] = "Holle Word!".getBytes();// 创建byte型数组
out.write(b);// 将数组中的信息写入到文件中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
// 判断out是否为空值,如果不等于空值,则需要将流关闭
try {
out.close();// 将流关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
word.txtファイルがプロジェクトディレクトリに表示されます。ファイル
をチェックインして、文をバイト配列に出力し
ます。FileInputStreamオブジェクトを作成して、ファイル内の情報を読み取ります
// 将文件信息读取输出
FileInputStream in = null;
try {
in = new FileInputStream(f);// 创建FileInputStream对象
byte b1[] = new byte[1024];//创建byte数组
int len = in.read(b1);//从文件中读取信息
System.out.println("文件中的信息是:" + new String(b1,0,len));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
// 判断in是否为空值,如果不等于空值,则需要将流关闭
try {
in.close();// 将流关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
完全なコード:
public class Study2 {
public static void main(String[] args) {
// 创建文件对象
File f = new File("word.txt");
// 将信息写入到文件中
FileOutputStream out = null;
try {
out = new FileOutputStream(f);// 创建FileOutputStream对象
byte b[] = "Holle Word!".getBytes();// 创建byte型数组
out.write(b);// 将数组中的信息写入到文件中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
// 判断out是否为空值,如果不等于空值,则需要将流关闭
try {
out.close();// 将流关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 将文件信息读取输出
FileInputStream in = null;
try {
in = new FileInputStream(f);// 创建FileInputStream对象
byte b1[] = new byte[1024];// 创建byte数组
int len = in.read(b1);// 从文件中读取信息
System.out.println("文件中的信息是:" + new String(b1, 0, len));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
// 判断in是否为空值,如果不等于空值,则需要将流关闭
try {
in.close();// 将流关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReaderおよびFileWriterクラス
FileInputStreamとFileOutputStreamの2つのクラスは、バイトまたはバイト配列を読み取るためのメソッドのみを提供するため、中国語の文字はバイト単位で2バイトを占めます。読み取りが適切でない場合、文字化けが発生する可能性があるため、 FileReader(ファイル文字入力ストリーム)およびFileWriter(ファイル文字出力ストリーム)文字ストリーム。
2つは似ており、メソッドは同じです。
コードは以下のように表示されます:
public class Study2 {
public static void main(String[] args) {
// 创建文件对象
File f = new File("word.txt");
// 将信息写入到文件中
FileWriter out = null;
try {
out = new FileWriter(f);// 创建FileOutputStream对象
String str = "Holle Word!";// 创建byte型数组
out.write(str);// 将数组中的信息写入到文件中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
// 判断out是否为空值,如果不等于空值,则需要将流关闭
try {
out.close();// 将流关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 将文件信息读取输出
FileReader in = null;
try {
in = new FileReader(f);// 创建FileInputStream对象
char c[] = new char[1024];// 创建byte数组
int len = in.read(c);// 从文件中读取信息
System.out.println("文件中的信息是:" + new String(c, 0, len));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
// 判断in是否为空值,如果不等于空值,则需要将流关闭
try {
in.close();// 将流关闭
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}