null第八章 输入/输出系统第八章 输入/输出系统 流与文件
标准
excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载
输入/输出
流输入/输出
§6.1 流和文件§6.1 流和文件流:数据从计算机的输入向输出流动,即流的产生。
流有两种:文本流(字符)和二进制流(字节)
在JAVA里,流是一些类。
文件也是一个逻辑概念。计算机的所有设备都可理解为一个文件。流可与文件建立联系。(File)外部设备输入流计算机内部输出流外部设备§6.1 流和文件--常见流类§6.1 流和文件--常见流类java.lang.Object java.io.File java.io.RandomAccessFile
java.io.InputStream
java.io.OutputStream
java.io.Reader
java.io.Writer
§6.2 标准输入/输出§6.2 标准输入/输出 标准输入输出属于系统类。系统类实现了用户使用资源时的系统无关编程接口:是最终类;所有变量和方法都是静态的;不用初始化(NEW)就可以使用
System.in
public final static InputStream in = new InputStream();
read(),read(byte b[],int off,int len),read(byte b[])
System.out
public final static PrintStream out = new PrintStream(); print(),println(),write()
System.err
public final static PrintStream err = new PrintStream(); print(),println(),write()§6.2 标准输入/输出(例)§6.2 标准输入/输出(例)import java.io.*;
public class Count{
public static void main(String args[]){
try{ int count = 0;
while (System.in.read()!=-1)
count++;
System.out.println("input has"+count+"chars");
}catch(IOException e){
System.err.println("Caught IOException:"+e.toString()); }
} }§6.3 文件File§6.3 文件File创建一个新的文件对象
File(String pathname)
File myFile = new File(〝etc/motd〞);
File(String parent,String child)
File myFile = new File(〝/etc 〞, 〝 motd〞);
File(File parent,String child)
File myDir = new File(〝/etc 〞);
File myFile = new File(myDir, 〝 motd〞);§6.3 文件File§6.3 文件FileFile类提供的方法
文件名的操作(如)
public String getName():返回文件对象名字符串;
public String toString():返回文件名字符串;
public String getParent():返回文件对象父路径名字符串;
public File getParentFile():返回文件对象父文件名;
public String getAbsolutePath():返回绝对路径名字符串;
public static File createTempFile(String prefix, String suffix,File directory) throws IOException
public static File createNewFile() throws IOException§6.3 文件File§6.3 文件File文件属性测试
public boolean canRead():是否能读指定的文件;
public boolean canWrite():是否能修改指定的文件;
public boolean exists():指定的文件是否存在;
public boolean isDirectory():指定的文件是否是目录;
public boolean isFile():指定的文件是否是一般文件;
public boolean isHidden():指定的文件是否是隐藏文件;
public boolean isAbsolute():指定的文件是否是绝对路径;§6.3 文件File§6.3 文件File
一般文件信息和工具
public long lastModified():返回文件最后被修改的时间;
public long length():返回文件的字节长度;
public boolean delete():删除指定的文件,若为目录必须为空才能删除;
public void deleteOnExit():当虚拟机执行结束时,删除指定文件或目录;
§6.3 文件File§6.3 文件File
目录操作
public boolean mkdir():创建指定的目录;
public boolean mkdirs():创建指定的目录,包括任何不存在的父目录;
public String[] list():返回指定目录下的文件;
public String[] list(FilenameFilter filter):返回指定目录下满足文件过滤器的文件;
§6.3 文件File§6.3 文件File
文件属性设置
public boolean setLastModified(long time):设置指定目录或文件的最后修改时间;
public boolean setReadOnly():设置指定目录或文件的只读属性;
§6.3 文件File§6.3 文件File其他
public URL toURL() throws MalformedURLException:将相对路径名存入URL文件;
public int compareTo(Object o):与另一个对象比较名字;
public boolean equals(Object obj):与另一个对象比较名字;
public int hashCode():返回文件名的哈希码;§6.3 文件File—例§6.3 文件File—例// FileTest.java
import java.io.*;
public class FileTest {
public static void main( String args[] ) {
if(args.length>0){
for (int i=0;i
记录
混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载
自己的过程叫对象串行化。
目的:支持Java的对象持续性,支持对象的远程方法调用(RMI),支持扩充和定制。
任务:写出对象实例变量的数值。如果变量是另一个对象的引用,则引用的对象也要串行化。
方法:只有实现了Serializable接口的类的对象才可以被串行化;该对象必须与一定的对象输入和输出流联系进行状态保存和状态恢复。§6.6 接口和对象串行化§6.6 接口和对象串行化接口Serializable和对象串行化(Serialization)
对象串行化
注意事项:串行化只能保存对象的实例成员变量的值;状态瞬时的对象不能串行化,保密的字段应加transient关键字。
定制串行化:默认的串行化机制,按照名称的升序写入其数值,如果想控制这些数值的写入顺序和写入类型,必须自己定义读写数据流的方式。§6.6 接口和对象串行化§6.6 接口和对象串行化//Student.java
import java.io.*;
public class Student implements Serializable{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department){
this.id = id;
this.name = name;
this.age = age;
this.department = department;}§6.6 接口和对象串行化§6.6 接口和对象串行化 private void writeObject(ObjectOutputStream out) throws IOException{
out.writeInt(id);
out.writeInt(age);
out.writeUTF(name);
out.writeUTF(department);}
private void readObject(ObjectInputStream in) throws IOException{
id = in.readInt();
age = in.readInt();
name = in.readUTF();
department = in.readUTF();} }§6.6 接口和对象串行化§6.6 接口和对象串行化//Objectser.java
import java.io.*;
public class Objectser {
public static void main(String args[]) throws IOException,ClassNotFoundException{
Student stu = new Student(20031010, " liming ",20, " CEIC");
FileOutputStream fout = new FileOutputStream("data.ser");
ObjectOutputStream sout = new ObjectOutputStream(fout);
try{sout.writeObject(stu);
sout.close();}catch(IOException e){
System.out.println(e.toString());} §6.6 接口和对象串行化§6.6 接口和对象串行化 stu = null;
FileInputStream fin = new FileInputStream(" data.ser");
ObjectInputStream sin = new ObjectInputStream(fin);
try{ stu = (Student)sin.readObject();
sin.close(); }catch(IOException e){
System.out.println(e.toString());}
System.out.println("Student info:");
System.out.println("ID :"+stu.id);
System.out.println("Name:"+stu.name);
System.out.println("Age:"+stu.age);
System.out.println("Dep.:"+stu.department);}}§6.7 随机存取文件RandomAccessFile§6.7 随机存取文件RandomAccessFile构造方法
RandomAccessFile(String name,String mode)
RandomAccessFile(File file,String mode)
使用方法
接口DataInput
接口DataOutput
getFilePointer():返回当前文件指针位置;
Seek(long pos):把文件指针定位于pos的位置;
Length():得到文件的长度.
§6.8 字符类输入流Reader§6.8 字符类输入流Readerjava.lang.Object PipedReader
CharArrayReader
java.io.Reader InputStreamReader -- FileReader
(抽象类) StringReader
FilteredReader-- PushBackReader
BufferedReader-- LineNumberReader §6.8 字符类输入流Reader§6.8 字符类输入流ReaderInputStreamReader ir = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
System.out.println("Input is : ");
String s = in.readLine();
InputStreamReader in = new InputStreamReader(new
FileInputStream(" input.txt "));
FileReader in = new FileReader (" input.txt ");
§6.9 字符类输出流Writer§6.9 字符类输出流Writerjava.lang.Object PipedWriter
CharArrayWriter
java.io.Writer OutputStreamWriter -- FileWriter
(抽象类) StringWriter
FilteredWriter
BufferedWriter
PrintWriter§6.8 字符类输出流Writer§6.8 字符类输出流WriterOutputStreamWriter ir = new OutputStreamWriter(System.out);
BufferedWriter out = new BufferedWriter(ir);
System.out.println(" Output is : ");
out.write(char[] cb, ,int off,int len);
OutputStreamWriter out = new OutputStreamWriter (new
FileOutStream(" output.txt "));
FileWriter out = new FileWriter (" output.txt ");小结小结基本的I/O
流与文件的概念
Java流类
读写一些基本流类的程序习题习题概念:字节流,字符流,输入流,输出流,顺序存取文件,随机存取文件以及各种派生的流类。
谈谈你对流的理解。二进制流和文本流的区别是什么?
使用in,out,err对象实现命令行的读写操作。
Java文件的顺序读取过程与输入/输出有什么关系?
请编写一个程序,分别显示某个目录下所有的文件和所有的目录,以及它们的属性。
利用文件输入/输出流完成:将文件exam.java的内容复制到文件test.txt中。