debugjavaMinor
Printing gitignores
Viewed 0 times
printinggitignoresstackoverflow
Problem
I have written a simple program in Java 7 to write the
```
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
public class Oye {
private static final String HELP_TXT = "help.txt";
private static final String DATA = "gitignore";
private HashMap map = new HashMap();
public Oye() {
// Eager approach, building the map in the constructor. Is it good?
File file = new File(DATA);
if (file.exists()) {
if (file.isDirectory()) {
findFiles(file.listFiles());
}
} else {
println("No data folder: " + DATA);
}
}
public static void main(String[] args) {
if (args.length == 0) {
printFile(HELP_TXT);
return;
}
Oye oye = new Oye();
if (args[0].equals("ls")) {
oye.listFiles();
} else {
printFile(oye.getFilePath(args[0]));
}
}
public void findFiles(File[] files) {
for (File file: files) {
if (file.isDirectory()) {
findFiles(file.listFiles());
} else {
String fileName = file.getName();
if (fileName.endsWith(".gitignore")) {
fileName = fileName.substring(0, fileName.indexOf(".gitignore"));
map.put(fileName, file.getAbsolutePath());
}
}
}
}
private String getFilePath(String name) {
if (map.containsKey(name)) {
return map.get(name);
} else {
throw new IllegalArgumentException("No such file: " + name);
}
}
private static void printFile(String path) {
Scanner scanner = null;
try {
scanner = new Scanner(new File(path));
String line;
while (scanner.hasNextLine()) {
println(scanner.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
private static void println(String txt) {
System.out.println(txt);
}
pr
.gitignore of different file types:```
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
public class Oye {
private static final String HELP_TXT = "help.txt";
private static final String DATA = "gitignore";
private HashMap map = new HashMap();
public Oye() {
// Eager approach, building the map in the constructor. Is it good?
File file = new File(DATA);
if (file.exists()) {
if (file.isDirectory()) {
findFiles(file.listFiles());
}
} else {
println("No data folder: " + DATA);
}
}
public static void main(String[] args) {
if (args.length == 0) {
printFile(HELP_TXT);
return;
}
Oye oye = new Oye();
if (args[0].equals("ls")) {
oye.listFiles();
} else {
printFile(oye.getFilePath(args[0]));
}
}
public void findFiles(File[] files) {
for (File file: files) {
if (file.isDirectory()) {
findFiles(file.listFiles());
} else {
String fileName = file.getName();
if (fileName.endsWith(".gitignore")) {
fileName = fileName.substring(0, fileName.indexOf(".gitignore"));
map.put(fileName, file.getAbsolutePath());
}
}
}
}
private String getFilePath(String name) {
if (map.containsKey(name)) {
return map.get(name);
} else {
throw new IllegalArgumentException("No such file: " + name);
}
}
private static void printFile(String path) {
Scanner scanner = null;
try {
scanner = new Scanner(new File(path));
String line;
while (scanner.hasNextLine()) {
println(scanner.nextLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
private static void println(String txt) {
System.out.println(txt);
}
pr
Solution
A few points that could be improved:
-
HELP_TXT: is it really worth readability to slim down your code down by a few bytes?HELP_TEXTis better.
- Same with
println's parameter:String txt\$\rightarrow\$String text
Oye oye = new Oye(): Sounds like a stereotypical Australian day, if this is the name of your script, perhaps consider a name change.GIT_IGNORERworks wonders in the understanding department.
fileName.substring(0, fileName.indexOf(".gitignore")): there's a magical tool called.replace:filename = filename.replace('.gitignore', '')
-
line is redundant and unused:try {
scanner = new Scanner(new File(path));
String line;
while (scanner.hasNextLine()) {
println(scanner.nextLine());
}Code Snippets
try {
scanner = new Scanner(new File(path));
String line;
while (scanner.hasNextLine()) {
println(scanner.nextLine());
}Context
StackExchange Code Review Q#101267, answer score: 4
Revisions (0)
No revisions yet.