Skip to content

Instantly share code, notes, and snippets.

@doct0rX
Created March 25, 2019 16:22
Show Gist options
  • Save doct0rX/cff57a9e7b905d193b337cf8057d99ef to your computer and use it in GitHub Desktop.
Save doct0rX/cff57a9e7b905d193b337cf8057d99ef to your computer and use it in GitHub Desktop.
script to make a new Indentation of any code file;
import java.util.List;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.IOException;
class Indent {
static List<String> records = new ArrayList<String>();
public static void main(String[] args) {
readFile("./ScanFragment.java");
System.out.println();
try {
PrintWriter writer = new PrintWriter("./New.java");
for (String line : records) {
writer.println(line);
}
writer.close();
} catch (IOException e) {
// do something
}
}
/**
* Open and read a file, and return the lines in the file as a list
* of Strings.
*/
static private List<String> readFile(String filename) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
int spaces = 0;
while (line.startsWith(" ")) {
line = line.replaceFirst(" ", "");
spaces++;
}
records.add(" ".repeat(spaces * 2) + line);
}
reader.close();
return records;
} catch (Exception e) {
System.err.format("Exception occurred trying to read '%s'.", filename);
e.printStackTrace();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment