简体   繁体   中英

How to write a converted to byte array text file content into another one in java

I converted a text file content to a byte array. I want to rewrite it into another text file but BufferedWriter writes it all in one line how can I fix it? I want that to be exactly like its original

this how I'm doing this:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class StringToBinary_Converter {
    public static ArrayList<byte[]> al = new ArrayList<byte[]>();

    public static void main(String[] args) {
        try {

            BufferedReader br = new BufferedReader(new FileReader(new File("A.txt")));
            String input;
            while ((input = br.readLine()) != null) {
                al.add(input.getBytes());

            }

            PrintStream ps = new PrintStream("B.txt");
            System.setOut(ps);

            for (int i = 0; i < al.size(); i++) {
                for (int j = 0; j < al.get(i).length; j++) {
                    System.out.println(al.get(i)[j]);
                }
            }

            for (int i = 0; i < al.size(); i++) {
                for (int j = 0; j < al.get(i).length; j++) {

    System.out.println(Integer.toBinaryString(Integer.parseInt(String.valueOf(al.get(i)[j]))));
                }
            }

            for (int i = 0; i < al.size(); i++) {
                for (int j = 0; j < al.get(i).length; j++) {

    System.out.println(Integer.toHexString(Integer.parseInt(String.valueOf(al.get(i)[j]))));
                }
            }

            try (BufferedWriter bw = new BufferedWriter(new FileWriter("C.txt"));) {
                for (int i = 0; i < al.size(); i++) {
                    bw.write(new String(al.get(i), StandardCharsets.UTF_8));
                }

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

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

    }

}

I'm using Eclipse and thanks in advance.

* Edit I know it's not needed a conversion to write a file content to another one.

Fix to this:

        while ((input = br.readLine()) != null) {
            al.add(input.getBytes());
            al.add('\n');

        }

You are reading by line, so the new line is lost, you need to add it again.

This would do all the work:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

public class StringToBinary_Converter{
    public static ArrayList<byte[]> al = new ArrayList<byte[]>();

    public static void main(String[] args) {
        try {

            BufferedReader br = new BufferedReader(new FileReader(new File("A.txt")));
            String input;
            while ((input = br.readLine()) != null) {
                al.add(input.getBytes());
            }

            PrintStream ps = new PrintStream("B.txt");
            System.setOut(ps);

            for (int i = 0; i < al.size(); i++) {
                String line = new String(al.get(i));
                for (int j = 0; j < al.get(i).length; j++) {
                    System.out.print((char)al.get(i)[j]);
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.getMessage();
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM