2013-08-26 39 views
2

我正在教導自己使用Java通過教科書進行編程。一個練習要求你寫格式化整個源代碼文件的程序(使用命令行):使用文本I/O重新格式化源代碼

從這個格式(下一行括號風格):

public class Test 
{ 
    public static void main(String[] args) 
    { 
    // Some statements 
    } 
} 

爲此格式(最終的線括號風格):

public class Test { 
    public static void main(String[] args) { 
    // Some statements 
    } 
} 

下面的代碼我到目前爲止:

import java.io.*; 
import java.util.*; 

public class FOURTEENpoint12 { 

public static void main(String[] args) throws IOException { 

    if (args.length != 2) { 
     System.out.println 
        ("Usage: java FOURTEENpoint12 sourceFile targetFile"); 
     System.exit(1); 
    } 

    File sourceFile = new File(args[0]); 
    File targetFile = new File(args[1]); 

    if (!sourceFile.exists()) { 
     System.out.println("File does not exist"); 
     System.exit(2); 
    } 

    Scanner input = new Scanner(sourceFile); 
    PrintWriter output = new PrintWriter(targetFile); 

    String token; 


    while (input.hasNext()) { 
     token = input.next(); 
     if (token.equals("{")) 
      output.println("\n{\n"); 
     else 
      output.print(token + " "); 

    } 

    input.close(); 
    output.close(); 
} 

}

我有兩個問題:

  1. 我曾試圖在塊上有許多不同,但不能得到任何接近什麼書是問。我嘗試過正則表達式,delimeters,令牌數組的不同組合,但仍然失敗。

  2. 本書要求您重新格式化單個文件,但該章從未解釋如何做到這一點。它只解釋瞭如何將文本重寫爲新文件。所以我只寫了程序來創建一個新文件。但我真的很想按照問題的方式去做。

如果有人能幫助我解決這兩個問題,它實際上可以解決很多練習中遇到的問題。提前致謝。

+0

恕我直言:你應用多行的正則表達式,所以你應該把整個文件放在一個字符串中,然後再使用它。另一個提示:你的正則表達式''''''''和它應該做的恰恰相反:) –

+0

'Scanner'默認使用一個或多個空格字符的分隔符。由於這是可變的,你將無法完全恢復。您可以將分隔符設置爲'\ n',但您可以使用'Scanner.hasNextLine/nextLine'或'BufferedReader'。然後你會檢查'token.matches(「\\ s * {\\ s *」)''。 – Dukeling

回答

1

這對於簡單的regular expression是可行的。

您想用) {替換) *any whitesplace character* {

在java中的正則表達式,有一個預定義的正則表達式:\s它匹配所有空格字符:[ \t\n\x0B\f\r]

在這裏,你想) {更換)\s*{

爲了實現這一目標,加載整個文件中的一個字符串(以下稱爲input),並使用String#replaceAll(String regex, String replacement)

//Added the escape characters... 
input.replaceAll("\\)\\s*\\{", ") {"); 

測試:

String input = "public static void main() \n {"; 
System.out.println(input.replaceAll("\\)\\s*\\{", ") {")); 

輸出:

public static void main() { 
1

如果我正確地閱讀了您的問題,您需要的只是跟蹤上一行和當前行。既然你學習,我只能給你大概的輪廓:

  • 如果當前行只包含括號和空白,然後忘了 當前行,增加空間和支柱上一行,並保持它作爲上一行。

  • 否則將前一行寫入輸出,將當前行設置爲前一行的 。

  • 讀取新的當前行並重覆上述操作,直到輸入文件結束。

嘗試更改您的代碼以匹配該代碼,逐行閱讀。

可以檢測是否行是{或者通過修整和比較"{",或與正則表達式等

​​(未測試)。

注意:這是最簡單的版本,它假定可移動的單獨在一條線上。

0

第一:hasNextLine()nextLine()似乎更適合閱讀線明智。

然後考慮要做些什麼。

閱讀:

line = " public static void main(String[] args)"; 

閱讀:

line = " {" 

寫:

public static void main(String[] args) { 

和其他一切必須被複制。

也許你想print(line)而不是println(line)並單獨確定一個println()?也許你需要記住你剛剛做了什麼boolean linePrintedWithoutLn = false;

0
import java.io.*; 
    import java.util.Scanner; 
    public class ReformatinCode { 
     public static void main(String[] args) throws Exception{ 
     File f1 = new File ("java8_19.txt"); 
     File f2 = new File("java8_19_result.txt"); 

     Scanner scr1 = new Scanner(f1); 
     StringBuffer sb1 = new StringBuffer(); 
     PrintWriter pw1 = new PrintWriter(f2); 
     while(scr1.hasNext()){ 
      sb1.append(scr1.nextLine() + System.lineSeparator()); 
     } 
     scr1.close(); 

     String output = format(sb1.toString()); 
     System.out.println(output); 
     pw1.print(output); 
     pw1.close(); 
    } 

public static String format(String s){ 
     String result =""; 
     result = s.replaceAll("\\\r\\n\\s\\{","\\{"); 
     return result; 
    } 

} 
/* this is a working answer , notice that the code that needed to be reformated is stored in the file "java8_19.txt"... and the file "java8_19_result.txt" will be created after running the code.... both will be in the workspace directory within the package folder 
相關問題