【实验来源】:
Java2程序设计基础(第2版)实验指导——清华大学出版社
【第10章:输入输出】:
* 实验49 FileInputStream的使用
* 2012.1.22 16:27
package
test;
import java.io.*;
public class ShowFile {
public static void main(String args[]) throws
IOException
{
int i;
FileInputStream fin=new
FileInputStream("myfile.txt");
do{
i=fin.read();
if(i!=-1)
System.out.print((char)i);
}while(i!=1);
fin.close();
}
}
* 实验50 FileOutputStream类的应用
* 2012.1.22 16:35
package
test;
import java.io.*;
public class Copyfile {
public static void main(String args[]) throws
IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
fin=new
FileInputStream("myfile.txt");//为fin实例化,指向的文件名为“myfile.txt”
fout=new
FileOutputStream("yourfile.txt");//为fout实例化,指向的文件名为“yourfile.txt”
do{
i=fin.read();
if(i!=-1)
fout.write(i);
}while(i!=1);
fin.close();
fout.close();
System.out.print("myfile.txt内容已经被复制到yourfile.txt中");
}
}
* 实验51 FileReader类的应用
* 2012.1.23 8:32
package
test;
import java.io.*;
public class ReadFile {
public static void main(String args[])throws
IOException
{
char[]c=new
char[500];//创建可容纳500个字符的数组
FileReader fr=new
FileReader("myfile.txt");
int
num=fr.read(c);//将数据读入字符数组c内,并返回读取的字符数
String str=new
String(c,0,num);//将字符串数组转换成字符串
System.out.println("读取的字符个数是:"+num+",其内容如下:");
System.out.println(str);
}
}
* 实验52 FileWrite类的应用
* 2012.1.23 8:39
package
test;
import java.io.*;
public class WriteFile {
public static void main(String args[])throws
IOException
{
FileWriter fw=new
FileWriter("test.txt");
String str1="广东金融学院";
String str2="欢迎使用java!";
fw.write(str1);
fw.write(str2);
fw.close();
System.out.println("内容已写入到文件test.txt中");
}
}
* 实验53 文件操作---新建文件程序
* 2012.2.23 10:38
package
test53;
import java.io.*;
public class File1 {
public static void main(String args[]) throws
IOException
{
//这里需要带参执行程序,参数为需要创建的一个或多个文件名
if (args.length==0)
{
System.out.println("没有需要创建的文件");
System.exit(1);
}
for(int
i=0;i<args.length;i++)
{
new
File(args[i]).createNewFile();
}
}
}
* 实验53 文件操作---写入文件程序
* 2012.2.23 10:39
package
test53;
import java.io.*;
public class File2 {
public static void main(String args[]) throws
IOException
{
BufferedWriter out = new
BufferedWriter(new FileWriter("a.txt"));
out.write("广东金融学院");
out.newLine();
out.write("Java程序设计");
out.flush();
out.close();
}
}
* 实验53 文件操作---读取文件程序
* 2012.2.23 10:40
package
test53;
import java.io.*;
public class File3 {
public static void main(String args[]) throws
IOException
{
String thisLine;
BufferedReader in = new
BufferedReader(new FileReader("a.txt"));
while((thisLine=in.readLine())!=null)//每次读取一行,知道文件结束
System.out.println(thisLine);
in.close();
}
}
* 实验53 文件操作---获取文件信息程序
* 2012.2.23 10:40
package
test53;
import java.io.*;
import java.util.*;
public class File4 {
public static void main(String args[]) throws
IOException
{
if(args.length==0)
{
System.out.println("缺少文件名");
System.exit(1);
}
for(int
i=0;i<args.length;i++)
{
status(args[i]);
}
}
public static void status(String fileName) throws
IOException
{
System.out.println("---"+fileName+"---");
File f = new
File(fileName);//创建file类的对象
if(!f.exists())//测试文件是否存在
{
System.out.println("文件没有找到");
System.out.println();
return;
}
System.out.println("文件全名为"+f.getCanonicalPath());
String p =
f.getParent();//显示文件的父目录
if(p!=null)
System.out.println("Parent
directory:"+p);
if(f.canRead())
//测试文件是否可读
System.out.println("File
is readable.");
if(f.canWrite())
//测试文件是否可写
System.out.println("File
is writable.");
Date d = new Date();
d.setTime(f.lastModified());
System.out.println("Last
modified"+d);
if(f.isFile())
{
System.out.println("文件大小是"+f.length()+"bytes");
}else if(f.isDirectory())
{
System.out.println("它是目录");
}else
{
System.out.println("既不是文件,也不是目录");
}
System.out.println();
}
}
* 实验53 文件操作---查看目录内容程序
* 2012.2.23 10:41
package
test53;
import java.io.*;
public class File5 {
public static void main(String args[]) throws
IOException
{
*查看当前目录内容
String[] dir = new
java.io.File(".").list();
java.util.Arrays.sort(dir);
for(int
i=0;i<dir.length;i++)
{
System.out.println(dir[i]);
}
*查看系统驱动器列表
File[] drives =
File.listRoots();
for(int
i=0;i<drives.length;i++)
{
System.out.println(drives[i]);
}
}
}
* 实验53 文件操作---删除文件程序
* 2012.2.23 10:42
package
test53;
import java.io.*;
public class File6 {
public static void main(String args[]) throws
IOException
{
File target = new
File("a.txt");
if(!target.exists())
System.out.println("文件不存在");
else
if(target.delete())
System.out.println("文件被删除");
else
System.out.println("文件不能删除");
}
}
* 实验54 读写基本类型数据
* 2012.1.23 11:03
package test;
import java.io.*;
public class BaseData {
public static void main(String args[]) throws
IOException
{
FileOutputStream fout;
DataOutputStream dout;
FileInputStream fin;
DataInputStream din;
File f = new
File("baseData.txt");
try
{
f.createNewFile();
}
catch(IOException e){}
try
{
fout = new
FileOutputStream(f);
dout = new
DataOutputStream(fout);
dout.writeInt(10);
dout.writeLong(12345);
dout.writeFloat(3.1415926f);
dout.writeDouble(987654321.123);
dout.writeBoolean(true);
dout.close();
}
catch(IOException e){}
try
{
fin = new
FileInputStream(f);
din = new
DataInputStream(fin);
System.out.println(din.readInt());
System.out.println(din.readLong());
System.out.println(din.readFloat());
System.out.println(din.readDouble());
System.out.println(din.readBoolean());
din.close();
}
catch(FileNotFoundException
e)
{
System.out.println("文件未找到!!!");
}
catch(IOException e){}
}
}
* 实验55 对象的写入与读取
* 2012.1.23 11:46
package
test53;
import java.io.*;
class Student implements Serializable
{
String name;
int age;
String dept;
public Student(String newName,int newAge,String
newDept)
{
name=newName;
age=newAge;
dept=newDept;
}
public String toString()
{
return
name+""+age+""+dept;
}
}
public class ReadWriteObject {
public static void main(String args[]) throws
IOException
{
Student w1 = new
Student("张三",20,"计算机系");
Student w2 = new
Student("李四",21,"金融系");
FileOutputStream fout;
ObjectOutputStream dout;
FileInputStream fin;
ObjectInputStream din;
File f = new
File("ReadWriteObject.txt");
try
{
f.createNewFile();
}
catch(IOException e)
{
System.out.println(e);
}
try
{
fout = new
FileOutputStream(f);
dout = new
ObjectOutputStream(fout);
dout.writeObject(w1);
dout.writeObject(w2);
dout.close();
}
catch(IOException e)
{
System.out.println(e);
}
try
{
fin = new
FileInputStream(f);
din = new
ObjectInputStream(fin);
Student r1 =
(Student)din.readObject();//从文件中读入对象到r1
Student r2 =
(Student)din.readObject();
System.out.println(r1);//显示r1对象的信息
System.out.println(r2);//显示r2对象的信息
din.close();
}
catch(IOException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
* 实验56 对文件的随即访问
* 2012.1.23 12:16
package
test;
import java.util.Scanner;
import java.io.*;
public class MyRandom {
public static void main(String args[])
{
//新建RandomFile.txt文件
File f = new
File("RandomFile.txt");
try
{
f.createNewFile();
}
catch(IOException e)
{
System.out.println(e);
}
//往文件中输入信息
String
str="qwertyuiopasdfghjklzxcvbnm";
try
{
FileWriter fw
= new FileWriter(f);
fw.write(str);
fw.close();
}
catch(IOException e)
{
System.out.println(e);
}
//从键盘中输入0~25的整数
int a=-1;
Scanner reader=new
Scanner(System.in);
while(a<0|a>25)
{
System.out.println("从键盘中输入0~25的整数:");
a=reader.nextInt();
}
//随机访问文件中的字符
try
{
RandomAccessFile
inFile =new RandomAccessFile("RandomFile.txt","r");
inFile.seek(a);//将文件指针移动到整数a位置
char c=inFile.readChar();//在inFile中读入一个字符
inFile.close();
System.out.println("RandomFile.txt文件中第"+a+"个字符是"+c);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
加载中,请稍候......