Work on lab6
This commit is contained in:
parent
fe2db10f23
commit
50674e0cef
@ -0,0 +1,2 @@
|
||||
John T Smith 90
|
||||
Eric K Jones 85
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class ReadFileFromWeb {
|
||||
public static void main(String[] args) {
|
||||
System.out.print("Enter a URL: ");
|
||||
String URLString = new Scanner(System.in).next();
|
||||
try {
|
||||
java.net.URL url = new java.net.URL(URLString);
|
||||
int count = 0;
|
||||
String text ="";
|
||||
Scanner input = new Scanner(url.openStream());
|
||||
while (input.hasNext()) {
|
||||
String line = input.nextLine();
|
||||
count += line.length();
|
||||
text += line;
|
||||
}
|
||||
System.out.println("The file size is " + count + " characters.");
|
||||
System.out.println(text);
|
||||
} catch (java.net.MalformedURLException ex) {
|
||||
System.out.println("Invalid URL");
|
||||
}
|
||||
catch (java.io.IOException ex) {
|
||||
System.out.println("IO Errors!");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WriteWithAutoClose {
|
||||
public static void main(String[] args) throws IOException
|
||||
{
|
||||
java.io.File file = new java.io.File("scores.txt");
|
||||
if (file.exists()) {
|
||||
System.out.println("File already exists!");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
//throw new IOException(); //Just for testing :)
|
||||
|
||||
try (java.io.PrintWriter output = new java.io.PrintWriter(file);) {
|
||||
// Write formatted output to the file
|
||||
output.print("John T Smith ");
|
||||
output.println(90);
|
||||
output.print("Eric K Jones ");
|
||||
output.println(85);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.mycompany.lab6.exceptions_calebfontenot;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WriteWithAutoClose2 {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
java.io.File file = new java.io.File("scores.txt");
|
||||
if (file.exists()) {
|
||||
System.out.println("File already exists!");
|
||||
System.exit(0);
|
||||
}
|
||||
//throw new IOException(); //Just for testing :)
|
||||
|
||||
try (java.io.PrintWriter output = new java.io.PrintWriter(file);) {
|
||||
// Write formatted output to the file
|
||||
output.print("John T Smith ");
|
||||
output.println(90);
|
||||
output.print("Eric K Jones ");
|
||||
output.println(85);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.err.println("Cought exception: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user