您的位置:首页 > 其它

IO输入输出 学习

2011-05-04 18:58 239 查看
IO/输入输出
判断某个文件是否存在,存在则删除,不存在则创建.

File类
1,它是IO包中唯一代表磁盘文件本身信息的类,而不是文件中内容。
2,它定义了一些与平台无关的方法来操纵文件,例如:创建,删除文件和重命名文件。
3,java中的目录被当作一种特殊的文件使用,list方法可以返回目录中的所有子目录和文件名。
4,在Unix下的路径分隔符为(/),在Dos下的路径分隔符为(/),java可以正确处理Unix和Dos的路径分隔符。
*/

java代码:
package com.zhangxiaoxiang02.io;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class FileTest {

public static void main(String[] args) {
File f = new File("1.txt"); //创建文件
if(f.exists()){ //判断文件是否存在
f.delete(); //删除文件
}else{
try {
f.createNewFile(); //创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("File Name:"+f.getName());
System.out.println("File path:"+f.getPath());
System.out.println("File abs path 获取文件的绝对路径:"+f.getAbsolutePath());
System.out.println("File Parent 获取文件的父目录:"+f.getParent()); //abc//1.txt 则返回abc
System.out.println(f.exists() ? "exists" : "not exists");
System.out.println("判断文件是否可读:"+(f.canRead() ? "read" : "not read"));
System.out.println("判断文件是否可写:"+(f.canWrite() ? "write" : "not write"));
System.out.println("判断文件是否是目录:"+(f.isDirectory() ? "directory" : "not directory"));
System.out.println("获取文件的最后一次被修改的时间:" + new Date(f.lastModified()));
} //f.lastModified() 返回值 long 类型 Date 自动调用 .toString()
}



RandomAccessFile 类
1,它提供众多文件访问方法,支持“随机访问”方式
随机读写等长记录格式的文件时有很大的优势,
2,仅限于操作文件不能访问其它的IO设备,如网络,内存映像等。
3,两种构造方法:
new RandomAccessFile(f,"rw"); //读写方式
new RandomAccessFile(f,"r"); //只读方式
Emloyee.java
*/

java代码:s

package com.zhangxiaoxiang02.io;

public class Employee {
public String name = null;
public int age = 0 ;
public static final int LEN = 8;

public Employee(String name, int age){
if(name.length() > LEN){
name = name.substring(0,LEN);
}else{
while (name.length() < LEN) {
name += "/u0000";
}
}
this.name = name;
this.age = age;
}

public static void main(String[] args) {

}

}

package com.zhangxiaoxiang02.io;

import java.io.RandomAccessFile;

public class RandomFileTest {

public static void main(String[] args) {
Employee e1 = new Employee("张三", 20);
Employee e2 = new Employee("lisi", 21);
Employee e3 = new Employee("wangwu", 23);
try {
RandomAccessFile ra = new RandomAccessFile("employee.txt", "rw");
ra.writeChars(e1.name);
ra.writeInt(e1.age);
ra.writeChars(e2.name);
ra.writeInt(e2.age);
ra.writeChars(e3.name);
ra.writeInt(e3.age);
ra.close();

String strName = "";
RandomAccessFile raf = new RandomAccessFile("employee.txt", "r");
raf.skipBytes(Employee.LEN*2 + 4);
//len = raf.read(buf);
//strName = new String(buf,0,len);
for(int i=0;i<Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim()+ ":" + raf.readInt()); //trim 出去空格

raf.seek(0); //定位到文件开始处
strName = "";
for(int i=0;i<Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim()+ ":" + raf.readInt());

raf.skipBytes(Employee.LEN*2 + 4);
strName = "";
for(int i=0;i<Employee.LEN;i++){
strName += raf.readChar();
}
System.out.println(strName.trim()+ ":" + raf.readInt());

raf.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}





InputStream:
程序可以从中连续读取字节的对象叫输入流,在java中,用InputSream类来描述所有输入流的抽象概念
int read()
int read(byte[] b) 返回实际读取到的字节数 一个都读不到的时候也会阻塞
int read(byte[] b,int off,int len)
long skip(long n) 跳过
int available() 返回是否有可读数据
void mark(int readlimik) 标记
void reset() 让指针回到标记处
boolean markSupported()
void close()
有了垃圾回收器,为什么还要调用close方法?
java类实例对象, 系统中产生的资源

OutputStream:
程序可以从中连续写入字节的对象叫输出流,在java中,用OuputSream类来描述所有输出流的抽象概念
void write(int b)
void write(byte[] b)
void write(byte[] b,int off,int len)
void flush() 刷新
void close()
IO缓冲区

FileInputStream与FileOutputStream
FileInputStream和FileOutputStream类分别用来创建磁盘文件的输入和输出流对象,通过它们的构造

函数来指定文件路征和文件名
创建FileInputStream实例对象时,指定的文件应当是存在和可读的。创建FileOutStream实例对象时,

如果指定文件已经存在,这个文件的原内容将被覆盖清除。

对同一个磁盘文件创建FileInputStream对象的两种方式:
(1)FileInputStream inOne = new FileInputStream("hello.txt");
(2)File f = new File("hello.txt");
FileInputStream inTwo = new FileInputStream(f);
创建FileOutputStrem实例对象时,可以指定还不存在的文件名,不能指定一个已被其它程序打开了的

文件。影响其他程序的结果.
输入输出是相对程序而言 不是相对文件而言!

*/

java代码:
package com.zhangxiaoxiang02.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileStream {

public static void main(String[] args) {
try {
FileOutputStream out = new FileOutputStream("hello.txt");
out.write("www.it315.org".getBytes()); //获得字符串的字节数组
out.close();

byte[] buf = new byte[1024];
File f = new File("hello.txt");
FileInputStream in = new FileInputStream(f);
int len = in.read(buf);
in.close();
System.out.println(new String(buf,0,len));
} catch (Exception e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: