LoginSignup
0
0

読み込んだファイルをUTF8で出力する

Last updated at Posted at 2023-12-18
import java.io.*;

public class ChangeFileEncoding {

    public static void main(String[] args) {
        String inputFilePath = "input.txt";  // 元のテキストファイルのパス
        String outputFilePath = "output_utf8.txt";  // UTF-8に変更した後のファイルのパス

        try {
            // テキストファイルをUTF-8で読み込み
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilePath), "現在のエンコーディング"));

            // UTF-8で新しいファイルに書き込み
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFilePath), "UTF-8"));

            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }

            // ファイルを閉じる
            reader.close();
            writer.close();

            System.out.println("UTF-8に変換が完了しました。");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class CharsetExample {
    public static void main(String[] args) {
        // ファイルパスを指定してBufferedReaderを作成
        try (BufferedReader reader = new BufferedReader(new FileReader("ファイルのパス"))) {
            // ファイルから1行読み込む
            String line = reader.readLine();
            
            // 読み込んだ行がnullでない場合
            if (line != null) {
                // 文字コードを取得
                Charset charset = detectCharset(line.getBytes());
                System.out.println("Detected Charset: " + charset.displayName());
            } else {
                System.out.println("ファイルが空です。");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 文字コードを検出するメソッド
    private static Charset detectCharset(byte[] bytes) {
        if (bytes == null || bytes.length < 2) {
            return StandardCharsets.UTF_8; // デフォルトでUTF-8と仮定
        }
        if (bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
            return StandardCharsets.UTF_16LE;
        } else if (bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
            return StandardCharsets.UTF_16BE;
        } else if (bytes.length >= 3 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
            return StandardCharsets.UTF_8;
        } else {
            return StandardCharsets.UTF_8; // デフォルトでUTF-8と仮定
        }
    }
}
↓↓↓↓↓↓↓ あなたの記事の内容


import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;

public class TextFileEncodingConverter {

    public static void main(String[] args) {
        convertFileToUtf8("input.txt", "output_utf8.txt");
    }

    private static void convertFileToUtf8(String inputFilePath, String outputFilePath) {
        try {
            // ファイルをUTF-8で読み込み
            List<String> lines = Files.readAllLines(Path.of(inputFilePath), Charset.defaultCharset());

            // ファイルをUTF-8で書き込み
            Files.write(Path.of(outputFilePath), lines, Charset.forName("UTF-8"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

            System.out.println("ファイルの変換が完了しました。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


11 変換のためにソースファイル TXT を読み込みます
Converter converter = new Converter("inpu
t.txゼ);
11 ターゲット形式の変換オプションを準備PDF
ConvertOptions convertOptions = new FileTy
pe().fromExtension ("pdf") .getConvertOption
SO);
/1 PDF形式に変換
converter. convert ("output.pdf", convertOpt ions) ;
───────

↑↑↑↑↑↑↑ 編集リクエストの内容

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0