一.File类
I/O流可以对文件的内容进行操作,但对文件本身的一些常规操作无法通过I/O流实现,如文件的创建、删除、重命名等操作,判断硬盘上某个文件是否存在等操作,为此JDK在java.io包下提供了一个FIle类,该类封装了一个路径,并提供了一系列方法: 1.常用构造方法
File file = new File("File.txt");
System.out.println(file);
File file1 = new File("E:\\java\\javaSenior\\Day_08\\hello.txt");
System.out.println(file1);
File file2 = new File("E:\\java\\javaSenior", "Day_08");
System.out.println(file2);
File file3 = new File(file2, "java.txt");
System.out.println(file3);
路径分隔符:在java中"\ “需要转译,因此java程序中表示路径会多出来一个”\ "。 2.常用方法 (1)File类的获取功能
@Test
public void test01(){
File file = new File("hello.txt");
File file1 = new File("E:\\java\\javaSenior\\he.txt");
System.out.println(file.getAbsoluteFile());
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
System.out.println(file.getParent());
System.out.println(file.getName());
System.out.println(new Date(file.lastModified()));
System.out.println(file.length());
System.out.println("**********************************");
System.out.println(file1.getAbsoluteFile());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getPath());
System.out.println(file1.getParent());
System.out.println(file1.getName());
System.out.println(new Date(file1.lastModified()));
System.out.println(file1.length());
File file2 = new File("E:\\java\\javaSenior");
String[] list = file2.list();
for(String s : list){
System.out.println(s);
}
System.out.println("---------------------------------");
File[] files = file2.listFiles();
for(File f : files){
System.out.println(f);
}
}
(2)File的重命名功能
@Test
public void test02(){
File file = new File("E:\\java\\javaSenior\\hello.txt");
File file1 = new File("E:\\JAVA EE\\java.txt");
boolean b = file.renameTo(file1);
System.out.println(b);
}
(3)File的判断功能
@Test
public void test03(){
File file = new File("E:\\JAVA EE\\java.txt");
System.out.println(file.exists());
System.out.println(file.isFile());
System.out.println(file.isDirectory());
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.isHidden());
}
(4)File类的创建、删除功能
@Test
public void test04() throws IOException {
File file = new File("E:\\JAVA EE\\java_EE.txt");
if(file.exists()){
file.delete();
System.out.println("创建时文件存在,已被删除...");
}else{
file.createNewFile();
System.out.println("创建时文件不存在,现已创建...");
}
}
@Test
public void test05(){
File file = new File("E:\\JAVA EE\\IeaProjeect\\11\\22");
boolean mkdir = file.mkdir();
if(mkdir){
System.out.println("mkdir创建成功...");
}
boolean mkdirs = file.mkdirs();
if(mkdirs){
System.out.println("mkdirs创建成功...");
}
}
注意:创建时文件的路径会因为是单元测试或main()方法而不同。
public class FileReaderTest {
public static void main(String[] args) {
File file = new File("Day_09\\Hello.txt");
System.out.println(file.getAbsolutePath());
File file1 = new File("Hello.txt");
System.out.println(file1.getAbsolutePath());
}
@Test
public void test01(){
File file = new File("Hello.txt");
System.out.println(file.getAbsolutePath());
}
}
二.I/O流
I/O流(Input/Output)流,即输入输出流,是Java中输入输出的基础,用于处理设备之间的数据传输。如:读/写文件,网络通讯等。
对于文本文件(.txt,.java,.c,.cpp等),使用字符流处理。文本文件的复制也可使用字节流。 对于非文本文件(.jpg,.mp3 ,.mp4,.avi,.doc , .ppt等),使用字节流处理。
三.字符流的使用
1.FileReader字符流读入数据 读入时的目标文件必须创建,不存在时会报错。
@Test
public void test01(){
File file = new File("Hello.txt");
FileReader fileReader = null;
try{
fileReader = new FileReader(file);
int len = 0;
while ((len = fileReader.read())!=-1){
System.out.print((char)len);
}
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
if(fileReader!=null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上操作效率太低,对该方法改进如下:
@Test
public void test02() {
File file = new File("Hello.txt");
FileReader fileReader = null;
try{
fileReader = new FileReader(file);
char[] buffer = new char[1024];
int len = 0;
while((len=fileReader.read(buffer))!=-1){
for(int i=0;i<len;i++){
System.out.print(buffer[i]);
}
System.out.println();
java.lang.String s = new java.lang.String(buffer, 0, len);
System.out.print(s);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.FileWirter字符流写出数据 写出时的目标文件不存在也可以,会自动创建。
@Test
public void test03(){
File file = new File("HelloWorld.txt");
FileWriter fileWriter = null;
try{
fileWriter = new FileWriter(file,true);
fileWriter.write("I have a dream!\n");
fileWriter.write(97);
}catch (IOException e){
e.printStackTrace();
}finally {
if(fileWriter!=null){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.FileWriter和FileReader实现文件的复制操作
@Test
public void copytest(){
File file = new File("HelloWorld.txt");
File file1 = new File("copyHelloWorld.txt");
FileWriter fw = null;
FileReader fr = null;
try{
fw = new FileWriter(file1);
fr = new FileReader(file);
char[] cbuffer = new char[1024];
int len = 0;
while((len = fr.read(cbuffer))!=-1){
fw.write(cbuffer,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try{
fw.close();
}catch (IOException e){
e.printStackTrace();
}
try{
fr.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
四.字节流的使用
使用FileInputStream和FileOutputStream实现非文本的复制:
@Test
public void test03() {
String file1 = "C:\\Users\\Administrator\\Desktop\\面向对象\\代理模式.png";
String file2 = "C:\\Users\\Administrator\\Desktop\\面向对象\\代理模式1.png";
long start = System.currentTimeMillis();
InputOutputStreamCopy(file1,file2);
long end = System.currentTimeMillis();
System.out.println("复制用时:"+(end-start));
}
public void InputOutputStreamCopy(String file1, String file2){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] bytes = new byte[1024];
int len = 0;
while ((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try{
fos.close();
}catch (IOException e){
e.printStackTrace();
}
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
五.缓冲流的使用
字节流、字符流的操作效率不高,缓冲流可以提高对文件读取、写入的速度。 原因:内部提供了一个缓冲区 BufferedOutputStream和BufferedWriter还有个flush()方法,执行此方法,缓冲区的内容将被清空。 1.字节缓冲流实现文件的复制操作
@Test
public void test03(){
String file = "E:\\java\\javaSenior\\Day_09\\HelloWorld.txt";
String file1 = "E:\\java\\javaSenior\\Day_09\\HelloWorld03.txt";
long start = System.currentTimeMillis();
copyWithBuffer(file,file1);
long end = System.currentTimeMillis();
System.out.println("复制用时:"+(end-start));
}
public void copyWithBuffer(String file1, String file2){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len = 0;
byte[] bytes = new byte[1024];
while ((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(bos!=null){
try{
bos.close();
}catch (IOException e){
e.printStackTrace();
}
}
if(bis!=null){
try{
bis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
2.字符缓冲流实现文件的复制 里面有一个特有的readLine()方法(不换行),newLine()可用于换行。
@Test
public void test01(){
BufferedReader br = null;
BufferedWriter bw = null;
try{
br = new BufferedReader(new FileReader(new File("E:\\java\\javaSenior\\Day_09\\壁纸.jpg")));
bw = new BufferedWriter(new FileWriter(new File("E:\\java\\javaSenior\\Day_09\\壁纸02.jpg")));
int len = 0;
String data;
while ((data=br.readLine())!=null){
bw.write(data+"\n");
bw.write(data);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(bw!=null){
try{
bw.close();
}catch (IOException e){
e.printStackTrace();
}
}
if(br!=null){
try{
bw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
六.转换流
转换流属于字符流。目的是提供字节流和字符流的转换。 InputStreamReader:将一个字节输入流转换为字符输入流 OutputStreamWriter:将一个字节输出流转换为字符输出流 解码:字节、字节数组—>字符串、字符数组 编码:字符串、字符数组—>字节、字节数组
@Test
public void test02(){
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("E:\\java\\javaSenior\\Day_09\\Hello.txt"));
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
char[] buffer = new char[1024];
int len;
while ((len=isr.read(buffer))!=-1){
String str = new String(buffer,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis!=null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
@Test
public void InputStreamReaderTest(){
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
FileInputStream fis = new FileInputStream(new File("E:\\java\\javaSenior\\Day_09\\HelloWorld01.txt"));
FileOutputStream fos = new FileOutputStream(new File("E:\\java\\javaSenior\\Day_09\\HelloWorld04.txt"));
isr = new InputStreamReader(fis,"utf-8");
osw = new OutputStreamWriter(fos,"utf-8");
char[] buffer = new char[1024];
int len ;
while((len=isr.read(buffer))!=-1){
osw.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(isr!=null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练习
@Test
public void testSecret(){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream(new File("E:\\java\\javaSenior\\Day_09\\壁纸.jpg"));
fos = new FileOutputStream(new File("E:\\java\\javaSenior\\Day_09\\加密壁纸.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
for(int i=0;i<len;i++){
buffer[i] = (byte) (buffer[i] ^ 5);
}
}
}catch (IOException e){
e.printStackTrace();
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(fos!=null){
try{
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
|