您的位置:首页 > 编程语言 > Java开发

java--IO流

2016-10-01 10:10 274 查看

遍历目录

File dir = new File("e:\\java");

if(!dir.exists()){
throw new IllegalArgumentException("目录:" + dir + "不存在!");
}
if(!dir.isDirectory()){
throw new IllegalArgumentException(dir + "不是目录!");
}
//String[] filenames = dir.list();//返回结果是字符串数组,直接子目录的名称
File[] filenames = dir.listFiles();//返回结果是File,直接子目录的抽象
if(filenames != null && filenames.length > 0){
for(File f : filenames){
if(f.isDirectory()){
//递归
listDirectory(f);
}
else
System.out.println(f);
}
}


访问文件

(读和写)– RandomAccessFile类

File file = new File("e:\\javaio\\a.dat");
if(!file.exists())
file.createNewFile();

RandomAccessFile raf = new RandomAccessFile(file,"rw");
System.out.println(raf.getFilePointer());
raf.write('A');
System.out.println(raf.getFilePointer());
int i = 0x7fffffff;
raf.writeInt(i);
System.out.println(raf.getFilePointer());


运行结果:看到写入后指针位置的变化



字节流

public class IOUtil {
//读取指定文件内容,16进制输出到控制台,每10个byte换行
//单字节读取(不适合大文件)
public void printHex(String fileName) throws IOException{
FileInputStream in = new FileInputStream(fileName);
int b=0,i=1;
while((b = in.read()) != -1){
//单字节前补0(十六进制用两个字节表示)
if(b <= 0xf)
System.out.print("0");
System.out.print(Integer.toHexString(b) + "  ");
if(i % 10 == 0)
System.out.println();
i++;
}
in.close();
}

//批量读取
public void printHexByByteArray(String fileName) throws IOException{
FileInputStream in = new FileInputStream(fileName);
byte[] bytes = new byte[20 * 1024];//设置缓冲区数组
int b=0,j=1;
while((b = in.read(bytes,0,bytes.length)) != -1){
for(int i = 0 ; i < b ; i++){
if(bytes[i] <= 0xf){
System.out.print("0");
}
//byte占8位,int占32位,转换进制时去掉前24位,节省内存
System.out.print(Integer.toHexString(bytes[i] & 0xff) + " ");
if(j % 10 == 0)
System.out.println();
j++;
}
}

in.close();
}

//写文件
public void outPut1()throws IOException{
//文件不存在则直接创建,若存在则删除再创建,若不删除在后面写,则加true
FileOutputStream out = new FileOutputStream("e:\\javaio\\out.dat");
out.write('A');
int a = 10;
out.write(a);
byte[] bytes = "中国".getBytes();
out.write(bytes);
out.close();
}

//复制文件
public void copyFile(File srcFile,File destFile) throws IOException{
if(!destFile.exists())
throw new IllegalArgumentException("文件" + destFile + "不存在!");
if(!srcFile.isFile())
throw new IllegalArgumentException(srcFile + "不是文件!");
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] bytes = new byte[8*1024];
int b = 0;
while((b = in.read(bytes, 0, bytes.length)) != -1){
out.write(bytes,0,b);
out.flush();
}
in.close();
out.close();
}

public void outPut2() throws IOException{
DataOutputStream out = new DataOutputStream(new FileOutputStream("e:\\javaio\\out.dat"));
out.writeInt(-10);
}

//复制文件(用缓冲区)
public void copyFileByBuffer(File srcFile,File destFile) throws IOException{
if(!destFile.exists())
throw new IllegalArgumentException("文件" + destFile + "不存在!");
if(!srcFile.isFile())
throw new IllegalArgumentException(srcFile + "不是文件!");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
int c;
while((c = in.read()) != -1){
out.write(c);
out.flush();
}
in.close();
out.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: