Skip to content

Instantly share code, notes, and snippets.

@rjlutz
Last active February 21, 2017 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjlutz/e8b4015064624459989c7f2b5d386226 to your computer and use it in GitHub Desktop.
Save rjlutz/e8b4015064624459989c7f2b5d386226 to your computer and use it in GitHub Desktop.
File/Web IO and HTML Writing Example
public class BadTickerException extends IllegalArgumentException {
private static final long serialVersionUID = 1L;
public BadTickerException() {
super();
}
public BadTickerException(String s) {
super(s);
}
}
public class FileIO {
public static void main(String[] args) {
// open file, handle exceptions
File csv = new File("data/quotes.csv");
Scanner reader = null;
try {
reader = new Scanner(csv);
} catch (FileNotFoundException e) {
System.out.println("Sorry, the file " + csv.toString() +" doesn't exist. Exiting....");
System.exit(1);
}
// don't process header -- yahoo doesn't give us one
// process line, print out the ticker and price
while(reader.hasNext()) {
String s = reader.nextLine();
String[] items = s.split(",");
//String ticker = items[0].substring(1,items[0].length()-1);
String ticker = items[0].replaceAll("\"", "");
// if (items[1].equals("N/A")) {
// // create a new exception
// BadTickerException myException = new BadTickerException("Invalid Symbol");
// //throw it
// throw myException;
// }
System.out.println(ticker + ' ' + items[1]);
}
// close file
reader.close();
}
}
FB 92.77 9/25/2015 4:00pm -1.64 95.85 95.85 92.06 28961622
FTR 5.00 9/25/2015 4:00pm +0.17 4.85 5.11 4.85 28553188
MU 14.91 9/25/2015 4:00pm -0.43 15.50 15.59 14.65 28917267
MSFT 43.94 9/25/2015 4:00pm +0.03 44.59 44.73 43.76 29384601
ORCL 36.05 9/25/2015 4:01pm -0.01 36.20 36.32 35.91 19386998
CSCO 26.025 9/25/2015 4:00pm +0.615 25.850 26.295 25.800 37618273
AAL 40.24 9/25/2015 4:00pm -0.01 40.89 41.03 39.92 7524947
AAL 40.24 9/25/2015 4:00pm -0.01 40.89 41.03 39.92 7524947
SONC 24.53 9/25/2015 4:00pm -0.03 24.81 25.05 24.44 992608
NVDA 23.61 9/25/2015 4:00pm +0.17 23.73 24.07 23.38 9242268
SPSL N/A N/A N/A N/A N/A N/A N/A N/A
AAPL 114.71 9/25/2015 4:00pm -0.29 116.36 116.69 114.02 56151926
IBM 145.42 9/25/2015 4:01pm +1.01 145.55 146.27 144.53 3474429
public class WebIO {
public static void main(String[] args) {
String yahoo = "http://download.finance.yahoo.com/d/quotes.csv?" +
"s=FB,FTR,MU,MSFT,ORCL,CSCO,AAL,AAL,SONC,NVDA,AAPL,IBM&" +
"f=sl1d1t1c1ohgv&e=.csv";
// open web resource, process exceptions
Scanner reader = null;
try {
URL url = new URL(yahoo);
reader = new Scanner(url.openStream());
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// don't process header -- yahoo doesn't give us one
// process line, print out the ticker and price
while(reader.hasNext()) {
String s = reader.nextLine();
String[] items = s.split(",");
//String ticker = items[0].substring(1,items[0].length()-1);
String ticker = items[0].replaceAll("\"", "");
if (items[1].equals("N/A")) {
// create a new exception
BadTickerException myException = new BadTickerException("Invalid Symbol");
//throw it
throw myException;
}
System.out.println(ticker + ' ' + items[1]);
}
// close file
reader.close();
}
}
public class WritingExperiment {
public static void main(String[] args) throws FileNotFoundException {
// create a new file for writing
File html = new File("ascii.html");
PrintWriter writer = new PrintWriter(html);
// write html
//// Write the top html
System.out.println("starting ...");
writer.println("<html>");
writer.println("<h1>Consolidated ASCII Table</h1>");
writer.println("<table border=\"3\" cellpadding=\"3\" cellspacing='3'>");
writer.println("<thead>");
writer.println("<tr><th>");
writer.println("<p><strong>Decimal</strong></p>");
writer.println("</th><th>");
writer.println("<p><strong>Character</strong></p>");
writer.println("</th>");
writer.println("</tr>");
writer.println("</thead>");
writer.println("<tbody>");
//// Write iterative table part
for (int i = 65; i <= 90; i++ ) {
writer.println("<tr><td>");
writer.println("<p>" + i + "</p>");
writer.println("</td><td align=\"right\">");
writer.println("<p>"+ (char)i + "</p>");
writer.println("</td>");
writer.println("</tr>");
}
//// Write the end stuff in html
writer.println("</tbody>");
writer.println("</table>");
writer.println("</html>");
// close file that we were writing
writer.close();
System.out.println("done ...");
}
}
@rjlutz
Copy link
Author

rjlutz commented Feb 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment