简体   繁体   中英

Replace newlines with \n and tabs with \t

I have to Convert java source code from next-line curly-brace style to the end-of-line curly-brace style.

I found a way to do this with formatted String (with \\n and \\t) and it works perfectly, but I have to read the source code from File.

The following code

Next-Line Brace Style Code:

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

should be converted to:

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

Now, the way I approached this, is as follow:

The mentioned code with \\n and \\t is as follow (we name this String s2 )

public class Test\n" +
                "{\n" +
                "\tpublic static void main(String[] args)\n" +
                "\t{\n" +
                "\t\t// Some statements\n" +
                "\t}\n" +
                "}\n

I then wrote the following code, to convert it to end-of-line style.

s2 = s2.replaceAll("\\t\\{", "{");
StringBuilder stringBuilder = new StringBuilder(s2);

for (int i = 0; i < stringBuilder.length(); i++) {
    if (stringBuilder.charAt(i) == '{') {
        stringBuilder.deleteCharAt(i);
        stringBuilder.insert(i - 1, " {");
    }
}

This works as far as I checked.


Now the actual problem;

When I read from the file, the source code doesn't have any \\t and \\n and I would like to convert the source code to a string, which like above string (s2) has \\t and \\n.


Just to clarify, I use the following method to read from input:

File sourceFile = new File("oldFormattedSourceCode.txt");
String oldFormatString = "";
try (Scanner input = new Scanner(sourceFile);) {
    while (input.hasNext()) {
        oldFormatString += input.nextLine();
    }
    System.out.println("Old Format String is: " + oldFormatString.toString());
} catch (FileNotFoundException ex) {
    System.out.print(ex.getMessage());

}

Which gives me the following output

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

And this String is no use to me because it doesn't have \\n and \\t, which the code I wrote depends on these.

Please suggest.

Instead of using a Scanner to read your file, use a FileReader

try (FileReader input = new FileReader(sourceFile);) {
    int i = 0;
    StringBuffer sb = new StringBuffer();
    while ((i=input.read())!=-1) {
        sb.append((char)i);
    }
    oldFormatString = sb.toString();
    System.out.println("Old Format String is: " + oldFormatString.toString());
} catch (FileNotFoundException | IOException ex) {
    System.out.print(ex.getMessage());
}

You can read individual characters if you use a FileReader , hence you wont miss any characters.

All you need is just StringBuilder and check input file line by line:

private static String convert(File file) throws FileNotFoundException {
    final String lineSeparator = System.getProperty("line.separator");
    StringBuilder buf = new StringBuilder();

    try (Scanner scan = new Scanner(file)) {
        while (scan.hasNext()) {
            String line = scan.nextLine();

            if ("{".equals(line.trim()))
                buf.append(" {").append(lineSeparator);
            else {
                if (buf.length() > 0)
                    buf.append(lineSeparator);

                buf.append(line);
            }
        }

        return buf.toString();
    }
}

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