File IO Practice
import java.nio.file.Path;
import java.nio.file.Files;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.file.NoSuchFileException;
public class Copy
{
public static void main(String[] args)
{
Path p = Path.of(args[0]);
//try with resources closes the file for you
try(
BufferedReader br = Files.newBufferedReader(p);
)
{
String line = "";
while( (line = br.readLine()) != null)
{
System.out.println(line);
}
}
catch(NoSuchFileException ex)
{
System.err.printf("File %s not found.\n",
Path.toAbsolutePath(p));
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
import java.nio.file.Path;
import java.nio.file.Files;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
public class WC
{
public static void main(String[] args)
{
Path p = Path.of(args[0]);
//try with resources closes the file for you
try(BufferedReader br = Files.newBufferedReader(p);)
{
String line = "";
int words = 0;
int lines = 0;
int chars = 0;
while( (line = br.readLine()) != null)
{
//System.out.println(line);
lines++;
chars += 1 + line.length();
words += line.split("\\s+").length;
}
System.out.printf("chars: %s\n", chars);
System.out.printf("lines: %s\n", lines);
System.out.printf("words: %s\n", words);
}
catch(NoSuchFileException ex)
{
System.err.printf("File %s not found.\n", p);
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
import java.util.function.Predicate;
import java.nio.file.Path;
import java.nio.file.Files;
public class Filter
{
private Predicate predicate;
private Path filePath;
public Filter(Predicate predicate, Path filePath)
{
this.predicate = predicate;
this.filePath = filePath;
}
/**
* Puts all lines for which the predicate evaluates to true
* to stdout
*/
public void process()
{
}
/**
* Puts all lines for which the predicate evaluates to true
* to the path specified by dest
* @return true if file is written to successfully
* and false if it fails.
*/
public boolean process(Path dest)
{
return false;
}
}
Spelunking Time
Sooprize! It's a small-group project!
Groups will be formed using mateFools.py
.
Savor its delicious source code! It's ancient Python 2,
'cause that's all I have on this server.
#!/usr/bin/env python
from sys import argv
import random
with open(argv[1], "r") as fp:
roster = fp.readlines()
random.shuffle(roster)
roster = [k.strip() for k in roster]
with open("fools.html", "w") as fp :
fp.write("<table class=\"center-table\">\n")
fp.write("<tr><th>Partner I</th><th>Partner II</th></tr>\n")
n = len(roster)
for k in range(0, 2*(n//2), 2):
fp.write("<tr><td>%s</td><td>%s</td></tr>\n" %(roster[k], roster[k + 1]))
fp.write("</table>\n")
if len(roster) % 2 != 0:
odd_man_out = roster[len(roster) - 1]
fp.write("odd man out: " + odd_man_out + "\n")
fp.write("random row: %s\n"% (1 + random.choice(range(0, (n//2)))))
This table will give the groups. I will run this program right in class.
Partner I | Partner II |
---|---|
guduru21a | nnamdi21o |
boyer21n | ndla21j |
sortisio21n | nipp21a |
liu21d | liu22s |
li21j | motteler21s |
mcginnis21c | campbell21j |
xu21j | pittenger21c |
gandhi21a | chen21a |
delgado21t | mehta21a |
thomas21g | du21m |
Here is a little shell code; you can snag it on the left.
import java.nio.file.Path;
import java.nio.file.Files;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.file.NoSuchFileException;
public class Copy
{
public static void main(String[] args)
{
Path p = Path.of(args[0]);
//try with resources closes the file for you
try(
BufferedReader br = Files.newBufferedReader(p);
)
{
String line = "";
while( (line = br.readLine()) != null)
{
System.out.println(line);
}
}
catch(NoSuchFileException ex)
{
System.err.printf("File %s not found.\n",
Path.toAbsolutePath(p));
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}