2014-02-17 3 views
1

Я читаю данные из текстового файла [utf8 format] и сохраняю его в строке и конвертирую эту строку в шестнадцатеричный формат и сохраняю эту шестнадцатеричную строку в другой файл, но я хочу сохранить эту преобразованную шестнадцатеричную строку построчно. Как это сделать?Запись в строку по строке

 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()); 
+2

Показать код до сих пор. – PeterMmm

+0

Строка sCurrentLine; br = новый BufferedReader (новый FileReader ("demmo123.txt")); while ((sCurrentLine = br.readLine())! = Null) { \t sb1.append (sCurrentLine); \t} \t Строка ss = sb1.toString(); \t Сканер sc = новый сканер (ss); \t String helloWorldHex = toHexString (ss.getBytes()); \t file = new File ("demmo.txt"); \t fop = new FileOutputStream (файл); \t if (! File.exists()) { \t file.createNewFile(); \t} \t байт [] contentInBytes = helloWorldHex.getBytes(); fop.write (helloWorldHex.getBytes()); \t \t \t //fop.write(contentInBytes); \t \t \t fop.flush(); \t \t \t fop.close(); \t \t \t \t \t \t \t \t \t fop.write (helloWorldHex.getBytes()); – user3134139

+1

@ user3134139: Пожалуйста, добавьте свой код на свой вопрос. Используйте ссылку «edit». – PakkuDon

ответ

0

Для ввода данных:

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."); 
     } 
    } 

} 

это дает:

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

спасибо. Алексей. – user3134139

+0

Добро пожаловать. –

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 
Смежные вопросы