file c_test_00.txt should contain
123.c
126.c
127.c
file cs_test_00.txt should contain
124.cs
etc.
Anyways I probably failed. Couldn't figure out how to test the first one since we are given the bare main class and running the code doesn't seem to check it to the test case. Even though I was told it failed.
your output: no stdout expected
expected output: no stdout expected
fail.
ok.....
That being said, here's what I did.
public List<File> SeparateTheFiles(String filename) throws FileNotFoundException{
List<File> newFiles = new ArrayList<>();
File f = new File(filename);
@SuppressWarnings("resource")
Scanner reader = new Scanner(f);
Map<String, String> filemap = new HashMap<>();
while(reader.hasNextLine()) {
String data = reader.nextLine();
String value;
String ext = data.substring(data.indexOf(".") + 1);
if(filemap.containsKey(ext)) value = filemap.get(ext) + ',' + data;
else value = data;
filemap.put(ext, value);
}
for(Map.Entry<String, String> entry : filemap.entrySet()) {
String newFilename = entry.getKey() + "_" + filename;
File newFile = new File(newFilename);
try {
FileWriter writer = new FileWriter(newFile);
BufferedWriter bw = new BufferedWriter(writer);
String[] temp = entry.getValue().split(",");
for(String s : temp) {
bw.write(s);
bw.newLine();
}
bw.close();
} catch(IOException e) {}
newFiles.add(newFile);
}
return newFiles;
}
test
List<File> newFiles = SeparateTheFiles("separatefile.txt");
for(File f : newFiles) {
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
} catch(IOException e) {}
}public File GetRequestForGif(String filename) {
String newFilename = "gif" + "_" + filename;
File newFile = new File(newFilename);
try {
FileWriter writer = new FileWriter(newFile);
BufferedWriter bw = new BufferedWriter(writer);
File f = new File(filename);
@SuppressWarnings("resource")
Scanner reader = new Scanner(f);
while(reader.hasNextLine()) {
String data = reader.nextLine();
if(!data.toLowerCase().contains(".gif")) continue;
String[] t = data.split(" ");
if(!t[8].equals("200")) continue;
Pattern p = Pattern.compile("[^/]+\\.gif");
Matcher m = p.matcher(data.toLowerCase());
if(m.find()) {
bw.write(m.group());
bw.newLine();
}
}
bw.close();
} catch(IOException e) {}
return newFile;
}
Test
File newFile = GetRequestForGif("getrequestforgif.txt");
try {
FileReader fr = new FileReader(newFile);
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println(s);
}
} catch(IOException e) {}
``