java--IO流(前半部分)

package Files;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;


import org.junit.Test;


public class file {
/**
* mkdir()  mkdirs();
* 创建目录(文件夹)
*/
@Test
public void test1(){
//创建File对象
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/a/b/c/d");
f.mkdir();//创建一个单层目录(文件夹)
f.mkdirs();//创建多及目录
System.out.println("完成!!!");
}

/**
* 创建文件createNewFile()
*/
@Test
public void test2(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
try {
f.createNewFile();//创建文件

} catch (IOException e) {
System.out.println("IO异常");
e.printStackTrace();
}
}
@Test
public void test3() throws IOException{
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
f.createNewFile();
}

/**
* delete()删除文件
*/
@Test
public void test4(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
f.delete();//删除文件

}

/**
* exists()
* 判断文件是否存在
* 存在返回true,不存在返回false
*/
@Test
public void test5(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
System.out.println(f.exists());

}

/**
* isFile()
* 判断一个路径是否是文件
* 如果是文件则返回true,如果是文件夹返回false
*/
@Test
public void test6(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello");
System.out.println(f.isFile());
}


/**
* isDirectory()
* 判断一个路径是否是文件夹
* true=是文件夹
* false=是文件
*/
@Test
public void test7(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello");
System.out.println(f.isDirectory());
}
/**
* getName()
* 获取文件名称
*/
@Test
public void test8(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
System.out.println(f.getName());
}
/**
* getAbsolutePath()
* 获得一个文件的绝对路径
*/
@Test
public void test9(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello");
System.out.println("绝对路径是:"+f.getAbsolutePath());
}


/**
* length()
*获得一个文件的容量  (单位是字节)
* @throws IOException 

*/

/** 
* list()   返回值是String[]
* 获得一个路径下的所有文件及文件夹(获得到名称)
* 返回值是String[]

*/
@Test
public void test11(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14");
String[] files=f.list();
System.out.println(Arrays.toString(files));
}
/**
* listFiles   返回值是File[]
* 获得一个路径下的所有文件及文件夹(获得到绝对路径)
* 返回值是File[]

*/
@Test
public void test12(){
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14");
File[] files=f.listFiles();
System.out.println(Arrays.toString(files));
}


//***********可以把IO流比喻成水流**************************************
/**
* 从磁盘文件中读取内容到java程序(用控制台输出)
* @throws IOException
*/
@Test
public void test13() throws IOException{
//创建输入流对象,从文件中读取内容
FileInputStream fis=new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
int b=0;
while((b=fis.read())!=-1){
System.out.println((char)b);
}
fis.close();//关闭输入流,释放内存空间,解除文件占用
}
/**
* FileInputStream
* 缓冲buffer的作用
* @throws IOException
*/
@Test
public void test14() throws IOException{
//创建输入流对象,从文件中读取内容
FileInputStream fis=new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
byte[] bi=new byte[4];//缓冲buffer的作用
while(fis.read(bi)!=-1){//读取缓冲池bi
System.out.println(new String(bi));//转换成字符串类型
}
fis.close();//关闭输入流,释放内存空间,解除文件占用
}

/**
* FileOutputStream
* getBytes()   将字符串转换成字节数组
* 向磁盘文件中写入内容
* @throws IOException 
*/
@Test
public void test15() throws IOException{
//创建输出流对象,从文件中获取内容
FileOutputStream fos=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello1.txt");
//fos.write('c');//写入字符
fos.write("myjavaSE".getBytes());//将字符串转换成字节数组
fos.close();//关闭输入流,释放内存空间,解除文件占用
}

/**
* 拷贝磁盘中的文本文件
* 原文件是hello.txt
* 拷贝生成一个hello1.txt
* @throws IOException 
*/

@Test
public void test16() throws IOException{
FileInputStream fis=new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
FileOutputStream fos=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello1.txt");
int b=0;
while((b=fis.read())!=-1){
fos.write(b);
}
System.out.println("拷贝完成 ");
fos.close();
fis.close();//先用后关闭,后用先关闭(类似于数据结构中的“栈”)
}
/**
* 拷贝一张图片(速度太慢了)
* @throws IOException
*/

@Test
public void test17() throws IOException{
FileInputStream fis=new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/19.jpg");
FileOutputStream fos=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/test.jpg");
int b=0;
while((b=fis.read())!=-1){//读取字节
fos.write(b);//写入字节
}
System.out.println("拷贝完成");
fos.close();
fis.close();//先用后关闭,后用先关闭(类似于数据结构中的“栈”)
}

}




/**
* length()
*获得一个文件的容量  (单位是字节)
* @throws IOException 
*/
@Test
public void test10() throws IOException{
File f=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
f.createNewFile();
FileOutputStream fos=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
fos.write("dsafdsafdsfdsAaAAAABFDSAA".getBytes());
FileInputStream fis=new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
int b=0;
int counta=0;
int countA=0;
while((b=fis.read())!=-1){
if(b=='a'){
counta++;
}else if(b=='A'){
countA++;
}
}
System.out.println("a:"+counta);
System.out.println("A:"+countA);
fis.close();
fos.close();
}
@Test
public void testcopyAB() throws IOException{
File f1=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
File f2=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
f1.createNewFile();
f2.createNewFile();
FileOutputStream fos=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos,1024);
bos.write("fsadfdsffGREG".getBytes());
bos.close();
fos.close();

FileInputStream fis=new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedInputStream bis=new BufferedInputStream(fis,1024);
FileOutputStream fosB=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
BufferedOutputStream bosB=new BufferedOutputStream(fosB,1024);

int b=0;
while((b=bis.read())!=-1){
bosB.write(b);
}
System.out.println("拷贝完成 ");
bosB.close();
fosB.close();
bis.close();
fis.close();

}

@Test
public void testfilerwcopyABC() throws IOException{
File f11=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
File f22=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
f11.createNewFile();
f22.createNewFile(); 
FileWriter fw=new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedWriter bw=new BufferedWriter(fw,1024);
bw.write("我爱大自然~~~");
bw.newLine();
bw.write("sure");
bw.close();
fw.close();

FileReader fr=new FileReader("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedReader br=new BufferedReader(fr,1024);
FileWriter FW=new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
BufferedWriter BW=new BufferedWriter(FW,1024);

String str;
while((str=br.readLine())!=null){//读取文件,按行读取
BW.write(str);
BW.newLine();
}
System.out.println("拷贝完成 ");
BW.close();
FW.close();
br.close();
fr.close();
}



/**
* exists()
* 判断文件是否存在
* 存在返回true,不存在返回false
* @throws IOException 
*/
@Test
public void test() throws IOException{
File f=new File("d:\\b\\info.txt");
boolean mainflag=f.exists();
if(mainflag==true){
System.out.println("文件已经存在,不可以重复创建!");
}else if(mainflag==false){
File f1=new File("d:\\b");
f1.mkdir();//创建一个单层目录(文件夹)
File f2=new File("d:\\b\\info.txt");
f2.createNewFile();
System.out.println("字节容量占"+f2.length());
System.out.println("文件名是:"+f2.getName());
System.out.println("父目录为:"+f2.getParent());
System.out.println("finish");
}else{
System.out.println("error");
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值