1

I am reading data from text [utf8 format] file and store it in a string and convert that string into hexadecimal format and save this hexadecimal string into another file but I want to save this converted hexadecimal string line by line. How to do that?

        String sCurrentLine;
        br = new BufferedReader(new FileReader("demmo123.txt"));
        while ((sCurrentLine = br.readLine()) != null) {
            sb1.append(sCurrentLine);
        }
        String ss = sb1.toString();
        Scanner sc = new Scanner(ss);
        String helloWorldHex = toHexString(ss.getBytes());
        file = new File("demmo.txt");
        fop = new FileOutputStream(file);
        if (!file.exists()) {
            file.createNewFile();
        }
        byte[] contentInBytes = helloWorldHex.getBytes();
        fop.write(helloWorldHex.getBytes());
        fop.write(contentInBytes);
        fop.flush();
        fop.close();
        fop.write(helloWorldHex.getBytes());
AJ.
  • 4,526
  • 5
  • 29
  • 41
  • 2
    Show your code so far. – PeterMmm Feb 17 '14 at 13:06
  • String sCurrentLine; br = new BufferedReader(new FileReader("demmo123.txt")); while ((sCurrentLine = br.readLine()) != null) { sb1.append(sCurrentLine); } String ss=sb1.toString(); Scanner sc = new Scanner(ss); String helloWorldHex = toHexString(ss.getBytes()); file = new File("demmo.txt"); fop = new FileOutputStream(file); if (!file.exists()) { file.createNewFile(); } byte[] contentInBytes = helloWorldHex.getBytes(); fop.write(helloWorldHex.getBytes()); //fop.write(contentInBytes); fop.flush(); fop.close(); fop.write(helloWorldHex.getBytes()); – user3134139 Feb 17 '14 at 13:10
  • 1
    @user3134139: Please add your code to your question. Use the `edit` link. – PakkuDon Feb 17 '14 at 13:10
  • 1
    @user3134139 Don't post code in comment (it is almost impossible to read, not to mention that if you have `//...` comments there it will not be easy to just copy-paste it to IDEs). Instead [edit] your post and include it there. – Pshemo Feb 17 '14 at 13:11
  • In order to achieve this you will need to learn file io. It's java docs http://docs.oracle.com/javase/tutorial/essential/io/ – Daksh Shah Feb 17 '14 at 13:13

2 Answers2

0

For input data:

test string 1
test string 2
test string 3
test string 4


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Example {
    public static void main(String[] args) {

        try {
            File input = new File("C:\\temp\\input.txt");
            File output = new File("C:\\temp\\output.txt");

            Scanner scanner = new Scanner(input);
            PrintWriter printer = new PrintWriter(output);
            while(scanner.hasNextLine()) {
                String string = scanner.nextLine();
                StringBuilder stringBuilder = new StringBuilder(200);
                for(char ch: string.toCharArray()) {
                    if(stringBuilder.length() > 0) stringBuilder.append(' ');
                    stringBuilder.append(String.format("%04x", (int)ch));
                }
                printer.write(stringBuilder.toString());                    
                printer.write("\r\n");
            }
            printer.flush();
            printer.close();
        }
        catch(FileNotFoundException e) {
            System.err.println("File not found.");
        }
    }

}

it gives:

0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0031
0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0032
0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0033
0074 0065 0073 0074 0020 0073 0074 0072 0069 006e 0067 0020 0034
0
FileWriter writer = new FileWriter("outputfile.txt"); // use your file extension
String content = "My first line";
writer.write(content+"\n");
writer.flush();
//if you have array of string use for loop 
writer.close();
// you should use exception handing always and import relevant classes
Nandha
  • 128
  • 10