Java文件读写
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class lecture {
public static void main(String[] args) throws IOException {
// writing data to text file
try{
//overwrite "w"
PrintWriter out = new PrintWriter(new FileOutputStream("data.txt"));
out.println("Welcome to JAVA");
out.println(2020);
out.println(23.12);
out.close();
//add a number 100 into data.txt
PrintWriter out1 = new PrintWriter(new FileOutputStream("data.txt",true));
out1.println("Welcome to JAVA");
out1.println(2020);
out1.println(23.12);
out1.close();
}catch (IOException e){
System.out.println("Problem with file output");
}
// reading data from text file
try {
Scanner in = new Scanner(new FileInputStream("data.txt"));
while (in.hasNextLine()){
System.out.println(in.nextLine());
}
in.close();
}catch(FileNotFoundException e){
System.out.println("File was not found");
}
// write a program to store the exchange rate to the text file
//named currency.txt
//USD 0.245 EUR 0.205 GBP 0.184 AUD 0.332 THB 7.41
try{
//overwrite "w"
PrintWriter out = new PrintWriter(new FileOutputStream("currency.txt"));
out.println("USD 0.245\nEUR 0.205\nGBP 0.184\nAUD 0.332\nTHB 7.41\n");
out.close();
}catch (IOException e){
System.out.println("Problem with file output");
}
// write a program to calculate the following based on the exchange rate
//RMB1234 = USD
//RMB456 = AUD
//RMB999 = THB
System.out.println("RMB1234 = USD" + getCurrency(1234, "USD"));
System.out.println("RMB1234 = AUD" + getCurrency(456, "AUD"));
System.out.println("RMB1234 = THB" + getCurrency(999, "THB"));
// write data to binary file
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("binary.dat"));
out.writeUTF("Welcome to JAVA");
out.writeInt(2020);
out.writeDouble(23.12);
out.close();
}catch (IOException e){
System.out.println("Problem with file output");
}
// read data from binary file
try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("binary.dat"));
// SEQUENCE is important!!
System.out.println(in.readUTF());
System.out.println(in.readInt());
System.out.println(in.readDouble());
in.close();
}catch (FileNotFoundException e){
System.out.println("File was not found");
}catch (IOException e) {
System.out.println("Problem with file input");
}
// generate N random numbers within 10-100, N is within (20-30)
// and then store in a binary file number.dat
Random r = new Random();
final int MIN = 10, MAX = 100;
int N = r.nextInt(11) + 20;
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("number.dat"));
for(int i=1; i<=N; i++){
out.writeInt(r.nextInt(MAX - MIN + 1) + MIN);
}
out.close() ;
}catch (IOException e){
System.out.println("Problem with file output");
}
// read all the random numbers from number.dat
// display all the numbers, maximum and minimum
int num=0, max=0, min=0;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("number.dat"));
num = in.readInt();
min = num;
max = num;
System.out.print(num + " ");
try {
while (true) {
num = in.readInt();
if (min > num) {
min = num;
}
if (max < num) {
max = num;
}
System.out.print(num + " ");
}
} catch (EOFException e) { }
in.close();
}catch (FileNotFoundException e){
System.out.println("File was not found");
}catch(IOException e){
System.out.println("Problem with file input");
}
System.out.println("\nThe max is " + max);
System.out.println("\nThe min is " + min);
// File Class
File f = new File("data.txt");
File f1 = new File("data1.txt");
File f2 = new File("data2.txt");
if (f.exists()){
System.out.println("The file already exists");
System.out.println("Rename file to data1.txt");
System.out.println(f.renameTo(f1));
}
if (f2.exists()){
System.out.println("The file already exists");
System.out.println("Delete the file");
System.out.println(f2.delete());
}
}
public static double getCurrency(double a, String b){
double result = 0;
try{
Scanner in = new Scanner(new FileInputStream("currency.txt"));
while(in.hasNextLine()){
String currency = in.next();
double rate = in.nextDouble();
if (b.equals(currency)){
result = a*rate;
break;
}
in.nextLine();
}
in.close();
}catch (FileNotFoundException e){
System.out.println("File was not found");
}
return result;
}
}
import java.util.Random;
import java.util.Scanner;
public class lecture {
public static void main(String[] args) throws IOException {
// writing data to text file
try{
//overwrite "w"
PrintWriter out = new PrintWriter(new FileOutputStream("data.txt"));
out.println("Welcome to JAVA");
out.println(2020);
out.println(23.12);
out.close();
//add a number 100 into data.txt
PrintWriter out1 = new PrintWriter(new FileOutputStream("data.txt",true));
out1.println("Welcome to JAVA");
out1.println(2020);
out1.println(23.12);
out1.close();
}catch (IOException e){
System.out.println("Problem with file output");
}
// reading data from text file
try {
Scanner in = new Scanner(new FileInputStream("data.txt"));
while (in.hasNextLine()){
System.out.println(in.nextLine());
}
in.close();
}catch(FileNotFoundException e){
System.out.println("File was not found");
}
// write a program to store the exchange rate to the text file
//named currency.txt
//USD 0.245 EUR 0.205 GBP 0.184 AUD 0.332 THB 7.41
try{
//overwrite "w"
PrintWriter out = new PrintWriter(new FileOutputStream("currency.txt"));
out.println("USD 0.245\nEUR 0.205\nGBP 0.184\nAUD 0.332\nTHB 7.41\n");
out.close();
}catch (IOException e){
System.out.println("Problem with file output");
}
// write a program to calculate the following based on the exchange rate
//RMB1234 = USD
//RMB456 = AUD
//RMB999 = THB
System.out.println("RMB1234 = USD" + getCurrency(1234, "USD"));
System.out.println("RMB1234 = AUD" + getCurrency(456, "AUD"));
System.out.println("RMB1234 = THB" + getCurrency(999, "THB"));
// write data to binary file
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("binary.dat"));
out.writeUTF("Welcome to JAVA");
out.writeInt(2020);
out.writeDouble(23.12);
out.close();
}catch (IOException e){
System.out.println("Problem with file output");
}
// read data from binary file
try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("binary.dat"));
// SEQUENCE is important!!
System.out.println(in.readUTF());
System.out.println(in.readInt());
System.out.println(in.readDouble());
in.close();
}catch (FileNotFoundException e){
System.out.println("File was not found");
}catch (IOException e) {
System.out.println("Problem with file input");
}
// generate N random numbers within 10-100, N is within (20-30)
// and then store in a binary file number.dat
Random r = new Random();
final int MIN = 10, MAX = 100;
int N = r.nextInt(11) + 20;
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("number.dat"));
for(int i=1; i<=N; i++){
out.writeInt(r.nextInt(MAX - MIN + 1) + MIN);
}
out.close() ;
}catch (IOException e){
System.out.println("Problem with file output");
}
// read all the random numbers from number.dat
// display all the numbers, maximum and minimum
int num=0, max=0, min=0;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("number.dat"));
num = in.readInt();
min = num;
max = num;
System.out.print(num + " ");
try {
while (true) {
num = in.readInt();
if (min > num) {
min = num;
}
if (max < num) {
max = num;
}
System.out.print(num + " ");
}
} catch (EOFException e) { }
in.close();
}catch (FileNotFoundException e){
System.out.println("File was not found");
}catch(IOException e){
System.out.println("Problem with file input");
}
System.out.println("\nThe max is " + max);
System.out.println("\nThe min is " + min);
// File Class
File f = new File("data.txt");
File f1 = new File("data1.txt");
File f2 = new File("data2.txt");
if (f.exists()){
System.out.println("The file already exists");
System.out.println("Rename file to data1.txt");
System.out.println(f.renameTo(f1));
}
if (f2.exists()){
System.out.println("The file already exists");
System.out.println("Delete the file");
System.out.println(f2.delete());
}
}
public static double getCurrency(double a, String b){
double result = 0;
try{
Scanner in = new Scanner(new FileInputStream("currency.txt"));
while(in.hasNextLine()){
String currency = in.next();
double rate = in.nextDouble();
if (b.equals(currency)){
result = a*rate;
break;
}
in.nextLine();
}
in.close();
}catch (FileNotFoundException e){
System.out.println("File was not found");
}
return result;
}
}