「Java」BufferedWriterで文字列の内容を出力ストリームに書き出すサンプル

2021年8月23日

書式

public void write(String str)throws IOException
文字列を書き込みます。

public void newLine() throws IOException
改行文字を書き込みます

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.info;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class BufferedFile {
private static String FIS_NAME ="C:/study/test01.txt";
private static String FOS_NAME ="C:/study/test02.txt";
private static String charSet ="UTF-8";
public static void main(String[] args) throws IOException{
System.out.println("元ファイルのデータ下記");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(FIS_NAME), charSet))) {
String data;
int i=0;
while ((data = reader.readLine()) != null) {
i++;
System.out.println(i + "行: " + data);
}
}
/*ファイル1の内容をファイル2の内容に記入*/
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(FIS_NAME), charSet ));
BufferedWriter bwr = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(FOS_NAME), charSet ))) {
String line;
int n=0;
/*テキストファイルの行を読み込む*/
while ((line = reader.readLine()) != null) {
n++;
System.out.println(n+"行内容を記入");
/*文字列を書き込む*/
bwr.write(line);
/*改行文字を書き込む*/
bwr.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.arkgame.info; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class BufferedFile { private static String FIS_NAME ="C:/study/test01.txt"; private static String FOS_NAME ="C:/study/test02.txt"; private static String charSet ="UTF-8"; public static void main(String[] args) throws IOException{ System.out.println("元ファイルのデータ下記"); try (BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(FIS_NAME), charSet))) { String data; int i=0; while ((data = reader.readLine()) != null) { i++; System.out.println(i + "行: " + data); } } /*ファイル1の内容をファイル2の内容に記入*/ try (BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(FIS_NAME), charSet )); BufferedWriter bwr = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(FOS_NAME), charSet ))) { String line; int n=0; /*テキストファイルの行を読み込む*/ while ((line = reader.readLine()) != null) { n++; System.out.println(n+"行内容を記入"); /*文字列を書き込む*/ bwr.write(line); /*改行文字を書き込む*/ bwr.newLine(); } } catch (IOException e) { e.printStackTrace(); } } }
package com.arkgame.info;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class BufferedFile {

       private static String FIS_NAME ="C:/study/test01.txt";
       private static String FOS_NAME ="C:/study/test02.txt";
       private static String charSet ="UTF-8";
       
      public static void main(String[] args) throws IOException{
            
            System.out.println("元ファイルのデータ下記");
            try (BufferedReader reader = new BufferedReader(
                        new InputStreamReader(new FileInputStream(FIS_NAME), charSet))) {
                  String data;
                  int i=0;
                  while ((data = reader.readLine()) != null) {
                        i++;
                        System.out.println(i + "行: " + data);
                  }

            }
            /*ファイル1の内容をファイル2の内容に記入*/
            try (BufferedReader reader = new BufferedReader(
                        new InputStreamReader(new FileInputStream(FIS_NAME), charSet ));
                        BufferedWriter bwr = new BufferedWriter(
                                    new OutputStreamWriter(new FileOutputStream(FOS_NAME), charSet ))) {
                  String line;
                  int n=0;
                  /*テキストファイルの行を読み込む*/
                  while ((line = reader.readLine()) != null) {
                        n++;
                        System.out.println(n+"行内容を記入");
                        /*文字列を書き込む*/
                        bwr.write(line);
                        /*改行文字を書き込む*/
                        bwr.newLine();
                  }
            } catch (IOException e) {
                  e.printStackTrace();
            }
      }
}

実行結果
元ファイルのデータ下記
1行: study skill in arkgame
2行: become smart
3行: find good
4行: job
1行内容を記入
2行内容を記入
3行内容を記入
4行内容を記入

Java

Posted by arkgame