1)写
package com.jmpower.javase.io;
import java.io.*;
public class DataOutputStreamTest01 {
public static void main(String[] args) {
DataOutputStream dos=null;
try {
//创建数据专属的字节输出流
dos=new DataOutputStream(new FileOutputStream("data"));
//写数据
byte b=100;
short s=400;
int i=200;
long l=300;
float f=3.0f;
double d=3.14;
boolean sex=false;
char c='0';
//写
dos.writeByte(b);
dos.writeShort(s);
dos.writeInt(i);
dos.writeLong(l);
dos.writeFloat(f);
dos.writeDouble(d);
dos.writeBoolean(sex);
dos.writeChar(c);
//刷新
dos.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2)读
package com.jmpower.javase.io;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class DataInputStreamTest01 {
public static void main(String[] args) {
DataInputStream dis=null;
try {
dis=new DataInputStream(new FileInputStream("data"));
//开始读
byte b=dis.readByte();
short s=dis.readShort();
int i=dis.readInt();
long l=dis.readLong();
float f= dis.readFloat();
double d= dis.readDouble();
boolean sex= dis.readBoolean();
char c= dis.readChar();
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(sex);
System.out.println(c);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.标准输出流
package com.jmpower.javase.io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class PrintStreamTest {
public static void main(String[] args) {
//联合起来写
System.out.println("hello world!");
//分开写
java.io.PrintStream ps=System.out;
ps.println("hello zhangsan");
ps.println("hello lisi");
ps.println("hello wangwu");
//标准输出流不需要手动colse()关闭。
//可以改变标准输出流的输出方向吗?可以
try {
//标准输出流不再指向控制台,指向“log”文件
PrintStream printStream=new PrintStream(new FileOutputStream("log"));
//修改输出方向,将输出方向修改到"log"文件
System.setOut(printStream);
//再输出,输入到了"log"文件中
System.out.println("hello lisi");
System.out.println("hello zhangsan");
System.out.println("hello wangwu");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
3.File类的理解以及常用方法
1)
boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
boolean createNewFile() 当且仅当具有该名称的文件尚不存在时,原子地创建一个由该抽象路径名命名的新的空文件。
boolean mkdir() 创建由此抽象路径名命名的目录。
boolean mkdirs() 创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录。
String getParent() 返回此抽象路径名的父 null的路径名字符串,如果此路径名未命名为父目录,则返回null。
File getParentFile() 返回此抽象路径名的父,或抽象路径名 null如果此路径名没有指定父目录。
String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串。
String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串。
package com.jmpower.javase.io;
import java.io.File;
import java.io.IOException;
public class FileTest01 {
public static void main(String[] args) {
//创建一个File对象
File f1=new File("D:\file");
//判断是否存在
System.out.println(f1.exists()); // false
//如果D:file不存在,则以文件的形式创建出来
//如果D:\file不存在,则以目录的形式创建出来
//可以创建多重目录吗?
File f3=new File("C:\Users\Jm\Desktop\Java\学习笔记(Java)");
//获取文件的父路径
String fatherPath=f3.getParent();
System.out.println(fatherPath);// C:UsersJmDesktopJava
File parentFile=f3.getParentFile();
System.out.println("获得绝对路径: "+parentFile.getAbsolutePath());
File f4=new File("doSome");
System.out.println("绝对路径: "+f4.getAbsolutePath());
}
}
2)
String getName() 返回由此抽象路径名表示的文件或目录的名称。
boolean isDirectory() 测试此抽象路径名表示的文件是否为目录。
boolean isFile() 测试此抽象路径名表示的文件是否为普通文件。
long lastModified() 返回此抽象路径名表示的文件上次修改的时间。
long length() 返回由此抽象路径名表示的文件的长度。
package com.jmpower.javase.io;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileTest02 {
public static void main(String[] args) {
File f1=new File("C:\Users\Jm\Desktop\Java\学习笔记(Java)\day01.txt");
//获取文件名
System.out.println("文件名: "+f1.getName());
//判断是否是一个目录
System.out.println(f1.isDirectory());// false
//判断是否是一个文件
System.out.println(f1.isFile());// true
//判断文件最后一次修改时时间
long haoMiao=f1.lastModified();
Date time=new Date(haoMiao);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
String strTime=sdf.format(time);
System.out.println(strTime);
//获取文件的大小
System.out.println(f1.length());// 2155字节
}
}
3)
File[] listFiles() 返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件。
package com.jmpower.javase.io;
import java.io.File;
public class FileTest03 {
public static void main(String[] args) {
File f1=new File("C:\Users\Jm\Desktop\Java\学习笔记(Java)");
File[] files=f1.listFiles();
//foreach
for(File file:files)
{
//获取绝对路径
//System.out.println(file.getAbsolutePath());
//获取名字
System.out.println(file.getName());
}
}
}
4.目录拷贝
package com.jmpower.javase.io;
import java.io.*;
//程序BUG:拷贝源目录下的第一个文件无法拷贝
public class CopyAll {
public static void main(String[] args) {
//拷贝源
File srcFile=new File("D:\666\c语言");
//拷贝目标
File destFile=new File("C:\Users\Jm\Desktop\Java");
//调用方法拷贝
copyDir(srcFile,destFile);
}
private static void copyDir(File srcFile,File destFile){
if(srcFile.isFile())
{
//srcFile如果是一个文件的话,递归结束
//是文件需要拷贝
//....一边读一边写
FileInputStream in=null;
FileOutputStream out=null;
try {
//读这个文件
//D:666c语言算法模板map函数.txt
in=new FileInputStream(srcFile);
//写到这个文件中
//C:UsersJmDesktopJava666c语言算法模板map函数.txt
String path=(destFile.getAbsolutePath().endsWith("\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\")+srcFile.getAbsolutePath().substring(3);
out=new FileOutputStream(path);
//一边读一边写
byte[] bytes=new byte[1024*1024];//1MB
int readCount=0;
while((readCount=in.read(bytes))!=-1)
{
out.write(bytes,0,readCount);
}
//刷新
out.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return ;
}
//获取源下面的子目录
File[] files=srcFile.listFiles();
for(File file:files)
{
if(file.isDirectory())
{
//新建对应目录
//System.out.println(file.getAbsolutePath());
//D:666c语言算法模板 源目录
//C:UsersJmDesktopJava666c语言算法模板 目标目录
String srcDir=file.getAbsolutePath();
String destDir=(destFile.getAbsolutePath().endsWith("\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\")+srcDir.substring(3);
File newFile=new File(destDir);
if(!newFile.exists())
{
newFile.mkdirs();
}
}
else {
//保证拷贝目录下第一个是文件时,不会出现FileNotFound的异常
File newFile=new File((destFile.getAbsolutePath().endsWith("\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\")+srcFile.getAbsolutePath().substring(3));
if(!newFile.exists())
{
newFile.mkdirs();
}
}
//递归调用
copyDir(file,destFile);
}
}
}



