2013-04-09 3 views
2

я должен создать небольшую программу для текстового файла, как это:Форматирование текста в .txt файле

hello_this_is_a_test_Example_ 
this line has to go up because it has a space at the beginning 
this one too 
this is the next line because there's no space at the start 
this one has to connect with the first line 

Я надеюсь, вы понимаете.

Таким образом, в конце концов, он должен сохранить форматированный текст в файл, например так:

hello_this_is_a_test_Example_ this line has to go up because it has a space at the beginning this one too 
this is the next line because there's no space at the start this one has to connect with te upper again 

В основном, если линия имеет пробел в начале каждой строки, он должен соединиться с Верхняя строка. У меня уже есть GUI, чтобы выбрать оба файла, просто нужны алгоритмы. Заранее спасибо: D

У меня есть это в данный момент, но он помещает все в одну строку. Это не так:

public class Engine { 

    public void Process(String fileIn,String fileOut) throws IOException{ 
     System.out.println("[Info]--> Processign"); 
     System.out.println("[Info]--> FileIn = " + fileIn); 
     System.out.println("[Info]--> FileOut = " + fileOut); 
     FileWriter Writer = new FileWriter(fileOut); 

     BufferedReader br = new BufferedReader(new FileReader(fileIn)); 
     String line; 
     String modified; 
     while ((line = br.readLine()) != null) { 
     System.out.println(line); 
      Writer.write(line); 
     if(line.startsWith(" ")){ 
      modified = line.replaceAll("\n ", " "); 
      Writer.write(modified); 
     } 
     } 
     br.close(); 
     Writer.close();}  
    } 
+4

Что вы пробовали до сих пор, кроме написания супер enterprisey GUI для этого? Попробуйте что-нибудь самостоятельно и вернитесь, если вы все еще сталкиваетесь с ошибками или если что-то не понимаете. Так что это не «место моей домашней работы». – akluth

+2

Для чтения текстового файла по строкам см. [Этот вопрос] (http://stackoverflow.com/questions/3432970/import-textfile-and-read-line-by-line-in-java). Алгоритм поиска, если линия начинается с пробела и т. Д., Очень проста, вы должны быть в состоянии это выяснить самостоятельно. – Jesper

ответ

1

Попробуйте это:

String modified = yourString.replaceAll("\n ", " "); 

Попробуйте что-то вроде этого:

StringBuilder builder = new StringBuilder(); 
foreach (string line in Files.ReadAllLines(@"c:\myfile.txt")) 
{ 
    builder.append(line); 
} 
String modified = builder.toString().replaceAll("\n ", " "); 
FileWriter fw = new FileWriter(@"c:\myfile.txt"); 
fw.write(modified); 
fw.close(); 
+0

Спасибо за ответ, но его не работает. У меня это сейчас: – WarGodNT

+0

Вам нужно запустить это на sring, содержащем весь файл. – CloudyMarble

+0

спасибо, но не получите эту часть: foreach (строковая строка в файлах.ReadAllLines (@ "c: \ myfile.txt")) { builder.append (строка); } – WarGodNT