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

java io入门程序集合

2014-08-02 11:10 309 查看
</pre><pre name="code" class="java">
import java.io.*;

import javax.imageio.stream.FileImageOutputStream;
public class ioTest1 {
public static void main(String []args){

//创建一个文件对象
File f=new File("f:\\myCounter.txt");
//得到文件路径
System.out.println("文件路径"+f.getAbsolutePath());
//得到文件的大小
System.out.println("文件大小:"+f.length());
//得到文件是否刻度
System.out.println("可读"+f.canRead());

//创建文件
File f=new File("f:\\shj.txt");
if(!f.exists()){
//可以创建
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//已经存在
System.out.println("文件存在了!");
}

//创建文件夹
File f=new File("f:\\ff");
if(f.isDirectory()){
//存在

}else{
//创建
f.mkdir();
}

//查看文件夹下面有多少个文件
File f=new File("f:\\ff");
if(f.isDirectory()){
File lists[]=f.listFiles();
for(File i : lists){
System.out.println("文件名:"+i);
i.delete();//删除文件,需要路径
}
}
//file 没有读写的能力,所以需要InputStream

File f=new File("f:\\ff.txt");
FileInputStream fis=null;
try {
fis=new FileInputStream(f);
//定义一个字节数组,相当于缓存
byte []bytes=new byte[1024];
int n=0;//实际读取到的字节数
//循环读取

while((n=fis.read(bytes))!=-1){
//把字节转成String
String s=new String(bytes,0,n);
System.out.println(s);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭文件流,必须放这
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//outputStream流的使用

File f=new File("f:\\ff.txt");
FileOutputStream fos=null;
try {
fos=new FileOutputStream(f);
String s="孙helloworld\r\n";  //回车换行
String s1="sdfsfd";
//byte []bytes=new byte[1024];
//如果大的话就用,小的话就不用了。
fos.write(s.getBytes());

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

//拷贝图片
//必须用字节流,用字符流会出错。
File f=new File("f:\\1.png");
FileInputStream fis=null;
File f1=new File("f:\\ff\\2.png");
FileOutputStream fos=null;
try {
fos=new FileOutputStream(f1);
fis=new FileInputStream(f);
//定义一个字节数组,相当于缓存
byte []bytes=new byte[1024];
int n=0;//实际读取到的字节数
//循环读取

while((n=fis.read(bytes))!=-1){
//把字节转成String
//String s=new String(bytes,0,n);
fos.write(bytes);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭文件流,必须放这
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//把文件拷贝到另一个文件
FileReader fr=null;
FileWriter fw=null;

//创建fr对象
try {
fr=new FileReader("f:\\ff.txt");
fw=new FileWriter("f:\\ff\\ff.txt");
char c[]=new char[1024];
int i=0;
while((i=fr.read(c))!=-1){
//方法一
String s=new String(c,0,i);
char c1[]=new char[i];
//System.out.println(s);
s.getChars(0, i, c1, 0);
fw.write(c1);

//方法二
fw.write(c,0,i);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//为了提高效率引入了缓冲字符流
BufferedReader br=null;
BufferedWriter bw=null;
try {
//先创建FileReader对象,再newBufferedReader
FileReader fr=new FileReader("f:\\ff.txt");
br=new BufferedReader(fr);

//创建FileWriter对象
FileWriter fw=new FileWriter("f:\\ff\\aa.txt");
bw=new BufferedWriter(fw);
String s="";
while((s=br.readLine())!=null){
System.out.println(s+"11");
bw.write(s+"\t\n");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java