java I/O流

IO流

字符 是由 字节 构成 字节转换成字符 也就意味字节流可以转换成字符流

 

------------------使用流 必然会使用File 类 文件类 流都是用来操作文件的

构建文件对象 File(String pathname)

mkdir() 创建指定目录

createNewFile() 创建文件

delete() 删除 文件或者 目录

exists() 文件和目录是否存在

isDirectory() 是否是目录

isFile() 是否是文件

listFiles() 返回 File对象的数组

 

inputStream 使用

public class Test1 {

public static void main(String[] args) {

//将硬盘内容读取程序中 输入流

//1.创建文件对象

File f = new File("c:\\abc.txt");

//2.使用字节输入流 读取内容

try {

InputStream in = new FileInputStream(f);

//3.创建一个byte数组

byte bs[] = new byte[1024];

//4.读取内容放入到字节数组中

int b = in.read(bs);

//5.构建内容

System.out.println(new String(bs));

System.out.println(new String(bs, 0, b));

//6.记得关闭流

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

OutputStream使用

public class Test2 {

public static void main(String[] args) {

//1.创建文件对象 也就需要输出内容到哪个对象

File f = new File("c:\\abc.txt");

//2.创建字节输出流

try {

OutputStream out = new FileOutputStream(f);

//3. 创建需要输出的内容

String s = "dasdsadasd";

//4.将字符串转化为字节的数组

byte bs[] = s.getBytes();

//5.输出内容

out.write(bs);

//6.记得关闭流

out.flush();

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

-------------------------------字符流

 

Readr使用

 

public static void main(String[] args) {

File f =new File("c:\\abc.txt");

try {

Reader r = new FileReader(f);

int b = 0;

int index = 0;

char cs[] = new char[1024];

while((b=r.read())!=-1){

cs[index] = (char) b;

index++;

}

System.out.println(new String(cs));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

 

 

 

writer使用

 

public static void main(String[] args) {

File f =new File("c:\\abc.txt");

try {

Writer wr = new FileWriter(f, true);

String s = "abc";

wr.write(s);

wr.flush();

wr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

 

----------------------------------------------- BufferedReader

public static void main(String[] args) {

try {

File f =new File("c:\\abc.txt");

Reader r = new FileReader(f);

BufferedReader r1 = new BufferedReader(r);

String s = "";

while ((s=r1.readLine())!=null) {

System.out.println(s);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

BufferedReader使用

public static void main(String[] args) {

try {

File f =new File("c:\\abc.txt");

Reader r = new FileReader(f);

BufferedReader r1 = new BufferedReader(r);

String s = "";

while ((s=r1.readLine())!=null) {

System.out.println(s);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

写一个方法判断是否文件下面有abc.txt文件 (递归)

import java.io.File;

/**

* 查看某一目录下是否有一名为abc.txt的文件,采用递归方法,有则返回true

* @author Administrator

*

*/

public class Search_File {

public static boolean Search(File file, String name) throws NullPointerException{

boolean flag = false;

//将这一级目录下的文件夹和文件存到数组中

File[] files = file.listFiles();

for (int i = 0; i < files.length; i++) {

//判断该数组下的是否有文件,有文件就比较,之后删除

if(files[i].isFile()) {

if(files[i].getName().equals(name)) {

flag = files[i].delete();

System.out.println(1);

}

}

}

//由于删除导致数组改变为空了,所以要重新声明了,

files = file.listFiles();

for (int i = 0; i < files.length; i++) {

//

if(!files[i].isFile()) {

Search(files[i], name);

}

}

return flag;

}

public static void main(String[] args) throws NullPointerException{

File f = new File("c://abc");

System.out.println(Search(f, "abc.txt"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值