目次
2. FileOutputStream クラスを使用する手順
(2) FileOutputStreamクラスのオブジェクトを作成する
1. FileOutputStream クラスの役割
データをファイルに書き込む
2. FileOutputStream クラスを使用する手順
(1) 関連クラスのパッケージをインポートする
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
(2) FileOutputStreamクラスのオブジェクトを作成する
このとき、例外が表示されるので、赤い波線をクリックし、ショートカット キー alt+enter を使用して最初の例外を選択してメソッド シグネチャに例外を追加するか、自分で例外をメソッド シグネチャに追加します。
FileOutputStream クラス オブジェクトを作成できます
public class FileOutputStreamDemo {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
}
}
注意: バイト出力ストリーム オブジェクトを作成する場合、パラメータには文字列または File オブジェクトで表されるパスを指定できます。 ファイルが存在しない場合は、新しいファイルが作成されますが、その親パスが存在する必要があります。 ファイルがすでに存在する場合は、ファイルを空にします (つまり、新しく書き込まれたデータが以前の内容を上書きします)
(3) データの書き込み
write メソッドを呼び出してデータを書き込む
(a). void write(int b) は一度に 1 バイトのデータを書き込みます
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//IOException是FileNotFoundException的父类,因此只写IOException即可
FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
//传入的是int类型数据,但实际写到文件中的是整数在ASCII上对应的字符
fos.write(97);
//相同的,抛出异常
}
}
(b). void write(byte[] b)バイト配列データを一度に書き込みます
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
byte[] bytes1 = {97,98,99,100,101};
fos.write(bytes1);
//一次写入一个字节数组数据
}
}
(c). void write(byte[] b, int off, int len)バイト配列のデータの一部を一度に書き込みます
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
byte[] bytes1 = {97,98,99,100,101};
fos.write(bytes1,2,4);
//off:起始字节索引,len:写入的字节个数
}
}
改行書き込みデータ
改行書き込みデータを実装するには、改行文字を書き込むだけです
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
//创建一个字符串,内容为换行符
fos.write(97);
String str1 = "\r\n";//也可以只写\r或者\n但最好写完整
//调用getBytes()方法,将字符串中内容转换为字节数组
byte[] bytes1 = str1.getBytes();
fos.write(bytes1);
//同样的,可以用字符串的方式创建较长的数据,再调用getBytes()方法,转换为字节数组
String str2 = "asdfghjklqwert";
byte[] bytes2 = str2.getBytes();
fos.write(bytes2);
}
}
書き続ける
連続書き込み (ファイルの元のデータの後に新しいデータを書き込む) を実装したい場合は、ファイル出力ストリーム オブジェクトの作成時に 2 番目のパラメーター true を渡すだけで済みます。
継続できる
(4) シャットダウン
ストリームを使用した後は毎回ストリームを閉じます。つまり、プログラムとファイル間のリンクを切断します。
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt");
byte[] bytes1 = {97,98,99,100,101};
fos.write(bytes1,2,4);
//关流
fos.close();
}
}
以下は継続の完全なコードです。コードの残りの部分は基本的に終了フローを追加するだけです。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
//创建输出流对象
FileOutputStream fos = new FileOutputStream("D:\\新建文件夹\\a.txt",true);
//写入续写数据
String str = "asdfghjklqwert";
byte[] bytes = str.getBytes();
fos.write(bytes);
//关流
fos.close();
}
}