java.lang.Object
java.io.File
boolean |
canRead() |
boolean |
canWrite() |
int |
compareTo(File pathname) |
boolean |
createNewFile() |
static File |
createTempFile(String prefix, String suffix) |
static File |
createTempFile(String prefix, String suffix, File directory) |
boolean |
delete() |
void |
deleteOnExit() |
boolean |
equals(Object obj) |
boolean |
exists() |
File |
getAbsoluteFile() |
String |
getAbsolutePath() |
File |
getCanonicalFile() |
String |
getCanonicalPath() |
String |
getName() |
String |
getParent() |
File |
getParentFile() |
String |
getPath() |
int |
hashCode() |
boolean |
isAbsolute() |
boolean |
isDirectory() |
boolean |
isFile() |
boolean |
isHidden() |
long |
lastModified() |
long |
length() |
String[] |
list() |
String[] |
list(FilenameFilter filter) |
File[] |
listFiles() |
File[] |
listFiles(FileFilter filter) |
File[] |
listFiles(FilenameFilter filter) |
static File[] |
listRoots() |
boolean |
mkdir() |
boolean |
mkdirs() |
boolean |
renameTo(File dest) |
boolean |
setLastModified(long time) |
boolean |
setReadOnly() |
String |
toString() |
URI |
toURI() |
URL |
toURL() |
FileDemo |
import java.util.*; import java.io.*; public class FileDemo { public static void main(String []args)throws IOException{ Scanner scan=new Scanner(System.in); System.out.println("请输入文件或文件夹路径!"); String path=scan.next(); File file=new File(path); if(file.isFile()){ System.out.printf("%s是文件\n",file.getName()); System.out.printf("%s文件%s\n",file.getName(),file.canRead()?"可读":"不可读"); System.out.printf("%s文件%s\n",file.getName(),file.canWrite()?"可写":"不可写"); System.out.printf("该文件为%d个字节\n",file.length()); }else if(file.isDirectory()){ List<File> fileList=new ArrayList<File>(); System.out.printf("%s是文件夹\n",file.getName()); File[] files=file.listFiles(); for(File tempFile:files){ if(tempFile.isFile()){ fileList.add(tempFile); // System.out.printf("%s\n",tempFile.getName()); }else{ System.out.printf("&&&&[%s]&&&\n",tempFile.getName()); } } for(File temfile:fileList){ System.out.printf("%s\n",temfile.getName()); } }else{ System.out.printf("%s文件不存在",file.getName()); file.mkdirs();
} } } |
java.lang.Object
java.io.RandomAccessFile
一个字符两个字节
一个字节八位
Int类型占4个字节
文件存取通常是循序的,每在文件中存取一次,文件的读取位置就会相对于目前的位置前进一次.然而有时必须制定文件的某个区段进行读取或写入的动作,也就是进行随机存取(RandomAccess),即要能在文件中随意第移动读取位置.这时可以使用RandomAccessFile,使用它的seek()方法来指定文件存取的位置,指定的单位是字节.
为了移动存取位置时的方便,通常在随机存取文件中会固定每一个数据的长度.例如长度固定为每一个学生个人的数据,Java中并没有直接的方法可以写入一个固定长度数据,所以在固定每一个长度方面必须自行设计.
File file=new File(args[0]); //建立RandomAccessFile实例并以读写模式开启文件 RandomAccessFile randomAccessFile=newRandomAccessFile(file,"rw"); for(int i=0;i<students.length;i++){ //使用对应的write方法写入数据 randomAccessFile.writeChars(students[i].getName()); randomAccessFile.writeInt(students[i].getScore()); } |
//使用seek()方法操作存取位置 randomAccessFile.seek((num-1)*Student.size()); Student stuent=new Student(); //使用对应的read方法读出数据 stuent.setName(readName(randomAccessFile)); stuent.setScore(randomAccessFile.readInt()); System.out.println("姓名:"+student.getName()); System.out.println("分数:"+student.getScore()); //设定关闭文件 randomAccessFile.close(); private static String readName(RandomAccessFile randomAccessfile)throws IOException{ char[]name=new char[15]; for(int i=0;i<name.length;i++) name[i]=randomAccessfile.readChar(); //将空字符取代为空格符并传回 return new String(name).replace('\0',''); } |
java.lang.StringBuilder |
用来累加字符串,一个可变的字符序列。此类提供一个与 在 例如,如果 |
void |
close() |
FileChannel |
getChannel() |
FileDescriptor |
getFD() |
long |
getFilePointer() |
long |
length() |
int |
read() |
int |
read(byte[] b) |
int |
read(byte[] b, int off, int len) |
boolean |
readBoolean() |
byte |
readByte() |
char |
readChar() |
double |
readDouble() |
float |
readFloat() |
void |
readFully(byte[] b) |
void |
readFully(byte[] b, int off, int len) |
int |
readInt() |
String |
readLine() |
long |
readLong() |
short |
readShort() |
int |
readUnsignedByte() |
int |
readUnsignedShort() |
String |
readUTF() |
void |
seek(long pos) |
void |
setLength(long newLength) |
int |
skipBytes(int n) |
void |
write(byte[] b) |
void |
write(byte[] b, int off, int len) |
void |
write(int b) |
void |
writeBoolean(boolean v) |
void |
writeByte(int v) |
void |
writeBytes(String s) |
void |
writeChar(int v) |
void |
writeChars(String s) |
void |
writeDouble(double v) |
void |
writeFloat(float v) |
void |
writeInt(int v) |
void |
writeLong(long v) |
void |
writeShort(int v) |
void |
writeUTF(String str) |
RandomAccessFileDemo |
import java.util.*; import java.io.*; public class RandomAccessFileDemo{ public static void main (String []args )throws FileNotFoundException,IOException{ Student s1=new Student("tom",20); Student s2=new Student("Jack",30); Student s3=new Student("zhang3",20); Student s4=new Student("Liudehua",10); Student[] students={s1,s2,s3,s4}; Scanner scan=new Scanner(System.in); System.out.println("请输入一个文件路径!"); String path=scan.next(); //RandomAccessFile工具,与File文件建立关联,即打开File文件,以RW(读写)形式打开 RandomAccessFile raf=new RandomAccessFile(new File(path),"rw"); for(Student student:students){ raf.writeChars(student.getName()); raf.writeInt(student.getAge()); }
System.out.println("请选择1-4的学生"); int count=scan.nextInt(); raf.seek((count-1)*Student.getSize()); char[]chars=new char[10]; for(int i=0;i<10;i++){ chars[i]=raf.readChar(); } String name=String.valueOf(chars); int age=raf.readInt(); System.out.printf("第%d个学生的名字为%s,年龄为%d",count,name,age); raf.close(); } } |
java.lang.Object
java.io.InputStream
计算机中的数据都是以0与1的方式来存储,如果要在两个装置之间进行数据的存取,当然也是以0与1位的方式来进行,Java将数据于目的地及来源之间的流动抽象华为一个流(Stream),而流当中流动的则是位数据.
流里面放的都是0和1,由于位是由于0和1来表示的,所以叫做位流.
数据流动抽象化为一个串流(Stream)
InputStream是所有表示位输入串流的类之父类
System中的标准输入串流in对象就是一个InputStream类型的实例
OutputSream是所有表示位输出串流的类之父类
System中的标准输出串流对象out其类型是java.io.PrintSream,OutputStream的子类.
很少直接操作InputStream货OutputStream上的方法,这些方法比较低阶
通常会操作它们的子类:
System.out.println(“输入字符十进制表示:”+System.in.read());
建立FileInputSream或FileOutputStream的实例时,必须制定文件位置及文件名,实例被创建时文件的串流就会开启.
不适用串流时,必须关闭文件串流,以释放与串流相依的系统资源.
FileInputStream fileInputStream=new FileInputStream(new File(srgs[0])); FileOutputStream fileOutputStream=new FileOutputStream(new File(args[1])); ... fileInputStream.close(); fileOutputStream.close();
|
while(true) { if(fileInputStream.available()<1024) { //剩余的资料比1024少 //一位一位读出再写入目标文件 int remain=-1; while((remain=fileInputStream.read())!=-1) { fileOutputStream.write(remain); } break; }else { //从来源文件读取数据至缓存区 fileInputStream.read(buffer); //将数组数据写入目标文件 fileOutputStream.write(buffer); } } |
类 FileInputStream方法摘要
|
||||||||||||||||||
|
类 FileOutputStream方法摘要
|
||||||||||||||
|
import java.util.*; import java.io.*; public class FileStreamDemo{ public static void main(String[]args) throws FileNotFoundException,IOException{ Scanner scan=new Scanner(System.in); System.out.println("请输入你要复制的文件"); String inPath=scan.next(); System.out.println("请输入你要复制到哪里"); String outPath=scan.next(); File inFile=new File(inPath); //创建FileInputStream对象,与FIle文件关联; FileInputStream fis=new FileInputStream(inFile); //创建FileOutputStream对象,与outPath文件关联; FileOutputStream fos=new FileOutputStream(new File(outPath)); System.out.println("开始复制"); long startTime=System.currentTimeMillis(); /*for(int i=0;i<=inFile.length();i++){ int flag=fis.read(); fos.write(flag); }*/ /*int flag=-1; while((flag=fis.read())>=0){ fos.write(flag); }*/ byte[] buffer=new byte[1024*20]; while(true){ if(fis.available()<1024*20){ int flag=fis.read(); if(flag<0)break; fos.write(flag); } fis.read(buffer); fos.write(buffer); } long endTime=System.currentTimeMillis(); System.out.println("复制完毕"); System.out.printf("用时%d",endTime-startTime); fis.close(); fos.close(); } } |
java.io.InputStream
java.io.FilterInputStream
BufferedInputStream与BufferedOutputStream
BufferedInputStream的资料成员buf是个位数组,默认为2048字节
BufferedOutputStream的资料成员buf是个位数组,默认为512个字节.
BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(desFile)); System.out.println("复制文件:"+srcFile.length()+"字节"); while(bufferedInputStream.read(data)!=-1){ bufferedOutputStream.write(data); } //将缓冲区中的数据全部写出 bufferedOutputStream.flush(); //关闭串流 bufferedInputStream.close(); bufferedOutputStream.close(); |
类BufferedInputStream方法摘要 |
||||||||||||||||
|
类 BufferedOutputStream方法摘要 |
||||||
|
import java.util.*; import java.io.*; public class BufferedStreamDemo{ public static void main(String []args) throws FileNotFoundException,IOException{ Scanner scan=new Scanner(System.in); System.out.println("请输入文件路径!"); String inPath=scan.next(); System.out.println("请输入存放路径!"); String outPath=scan.next(); File inFile=new File(inPath);
BufferedInputStream bin=new BufferedInputStream(new FileInputStream(new File(inPath))); BufferedOutputStream bout=new BufferedOutputStream(new FileOutputStream(new File(outPath))); System.out.println("开始复制"); long startTime=System.currentTimeMillis(); byte[] data=new byte[1]; while((bin.read(data))!=-1){ bout.write(data); } long endTime=System.currentTimeMillis(); System.out.println("复制完毕"); System.out.printf("用时%d秒",(endTime-startTime)/1000); bin.close(); bout.close(); } } |
java.io.InputStream
java.io.FilterInputStream ![继承者]()
主要是对java的基本数据类型,以及字符串提供的一些读写操作
数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。
提供一些对java基本数据型态写入的方法 |
DataOutputStream dataOutputStream=new DataOutputStream(new FileOutputStream(args[0])); for(Member member:member) { //写入UTF字符串 dataOutputStream.writeUTF(member.getName()); //写入int资料 dataOutputStream.writeInt(member.getAge()); } //清除所有数据至目的地 dataOutputStream.flush(); //关闭串流 dataOutputStream.close(); |
DataInputStream dataInputStream=new DataInputStream(new FileInputStream(args[0])); //读出数据并还原为对象 for(int i=0;i<members.length;i++){ //读出UTF字符串 String name=dataInputStream.readUTF(); //读出int资料 int score=dataInputStream.readInt(); members[i]=new member(name,score); } //关闭串流 dataInputStream.close(); |
类 DataInputStream方法摘要 |
||||||||||||||||||||||||||||||||||||
|
类 DataOutputStream方法摘要 |
||||||||||||||||||||||||||||||
|
import java.util.*; import java.io.*; public class DataStreamDemo{ public static void main(String []args) throws Exception{ Student s1=new Student("Aldrich",25); Student s2=new Student("zhangsan",34); Student s3=new Student("lisi",23); Student s4=new Student("wangwu",12); Student s5=new Student("zhaoliu",21); Student[] Students={s1,s2,s3,s4,s5}; Scanner scan=new Scanner(System.in); System.out.println("请输入一个文件!"); String path=scan.next(); DataOutputStream dout=new DataOutputStream(new FileOutputStream(new File(path))); DataInputStream din=new DataInputStream(new FileInputStream(new File(path))); for(Student student:Students){ dout.writeUTF(student.getName()); dout.writeInt(student.getAge()); } dout.flush(); for(int i=0;i<5;i++){ String name=din.readUTF(); int age=din.readInt(); System.out.printf("第%d个学生的名字为%s,年龄是%d\n",i+1,name,age); } din.close(); dout.close(); } } |
java.io.InputStream
ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。通过使用流中的文件可以实现对象的持久存储。如果流是网络套接字流,则可以在另一台主机上或另一个进程中重构对象。
ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。
对对象直接进行读写操作
把能够实现Serializable接口的对象,表示这个对象可序列化或可串行化的
要直接储存对象,定义该对象的类必须实现java.io.Serializable接口
public class User implements Serializable
{
private static final long serialVersionUID=1L;
...
}
serialVersionUID代表了可串行化对象版本,从文件读回对象时两个对象的serialVersionUID不相同的话,就会丢出java.io.InvalidClassException
类 ObjectInputStream方法摘要
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
类 ObjectOutputStream方法摘要 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
import java.util.*; import java.io.*; public class ObjectStreamDemo{ public static void main(String[]args)throws Exception{ Student s1=new Student("tom",20); Student s2=new Student("Jack",30); Student s3=new Student("zhang3",20); Student s4=new Student("Liudehua",10); Student[]students={s1,s2,s3,s4}; Scanner scan =new Scanner(System.in); System.out.println("请输入一个路径!"); String path=scan.next(); ObjectOutputStream oout=new ObjectOutputStream(new FileOutputStream(new File(path))); ObjectInputStream oin=new ObjectInputStream(new FileInputStream(new File(path))); for(Student student:students){ oout.writeObject(student); } oout.flush(); for(int i=0;i<4;i++){ Student student=(Student)oin.readObject(); System.out.printf("第%d个学生的名字为%s,年龄为%d\n",i+1,student.getName(),student.getAge()); } oout.close(); oin.close(); } } |
java.util.Dictionary<K,V>
java.util.Hashtable<Object,Object>
Properties
类表示了一个持久的属性集。Properties
可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
属性文件的读写
以properties为扩展名的文件叫属性文件
key=value的形式(value不用用引号)
#为单行注释
java.util.Properties
load()加载属性文件
getProperty("key")获取value
类 Properties方法摘要 |
||||||||||||||||||||||
|
import java.util.Properties; import java.io.*; public class PropertiesDemo{ public static void main(String args[])throws Exception{ //构建Properties工具对象 Properties prop=new Properties(); //加载属性文件 prop.load(new FileInputStream(new File("d:\\src\\school.properties"))); String name=prop.getProperty("name"); String address=prop.getProperty("address"); System.out.println(name); System.out.println(address); } } |