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

JAVA基础,IO流。(文件复制的实现)

2014-07-15 00:04 489 查看
IO流是java中比较难学的,建议学习之前熟练掌握面向对象的相关。

1、file类。

文件的操作。

创建文件。删除文件。我们在删除文件之前应该考虑文件是否存在,存在的话就才可以删除掉。综合应用如下PS:创建文件夹与其类似,用mkdir();

import java.io.*;
public class file2
{
public static void main(String args[])throws IOException{
File a=new File("d:"+File.separator+"ad.txt");   //separator根据系统确定文件分隔符
if(a.exists()){         //存在就删除,不存在创建
a.delete();
}
else{
a.createNewFile();
}
}
}
列出某文件夹下所有文件,包括子目录。
import java.io.*;
public class file4
{
public static void main(String args[])throws IOException{
File a=new File("d:"+File.separator+"\\uxp");//separator根据系统确定文件分隔符
print(a);
}
public static void print(File file){
if(file!=null){           //判断路径是否为空
if(file.isDirectory()){ //判断系统路径存在否
File f[]=file.listFiles();
if(f!=null){
for(int i=0;i<f.length;i++){   //递归查询存在否。
print(f[i]);
}
}
}
else{
System.out.println(file);
}
}
}
}

2、InputStream,OutputStream类。

这两个类是属于字节流。分别是属于输入流和输出流。InputStream类是从文件里把内容读取出来。OutputStream类是输出流,把内容输出到文件中。

两个类的一般使用方法如下:

InputStream类要定义一个比特数组的大小,read方法只能读取字节数组。

import java.io.*;
public class inputStream
{
public static void main(String args[]) throws Exception{
File a=new File("d:"+File.separator+"d.txt"); //1,确定文件路径
InputStream in=new FileInputStream(a); //2、实例化父类。开启在末尾添加模式。否则默认会覆盖原内容
byte b[]=new byte[1024]; //把内容协警比特数组中。
int len=0;
int temp=0;
while((temp=in.read())!=-1){          //不知道文件的大小来使用的。
b[len]=(byte)temp;
len++;
}
System.out.println("内容为:"+new String(b,0,len));//按字节写内容
in.close();      //4、关闭文件
}
}
而OutputStream类也是类似的道理,write方法要读取byte数组,把添加的内容转换后进入数组中,然后输入到文件中。

import java.io.*;
public class outputStream
{
public static void main(String args[]) throws Exception{
File a=new File("d:"+File.separator+"d.txt"); //1,确定文件路径
OutputStream out=new FileOutputStream(a,ture); //2、实例化父类。开启在末尾添加模式。否则默认会覆盖原内容
String str="\r\nhello"; //添加后换行 定义了String来添加要添加的内容。
byte b[]=str.getBytes();
for(int i=0;i<b.length;i++)
{
out.write(b[i]);    //3、写内容,按字节来写内容。
}
out.write(b);    //按字节写内容
out.close();      //4、关闭文件
}
}
3、Reader、writeer类

此两类属于字符输出流。进行操作时是先放在内存区域先。然后在进行储存或提取操作。与字节流类似的是步骤都大同小异。

import java.io.*;
public class reader
{
public static void main(String args[])throws Exception{
File f=new File("d:"+File.separator+"dd.txt");
Reader out=new FileReader(f);
char c[]=new char[(int)f.length()];
int temp=0;   //作为读的标记。
int len=0;      //内容的长度不确定,随着读取而变化
while((temp=out.read())!=-1){
c[len]=(char)temp;        //边读边写,既可以防止定义的数组太小也可以防止定义太大对系统的空间造成浪费。
len++;
}
System.out.println(new String(c));
out.close();
}
}
写操作也是类似于outputStream的。

import java.io.*;
public class writer
{
public static void main(String args[])throws Exception{
File f=new File("d:"+File.separator+"dd.txt");
Writer out=new FileWriter(f.true); //末尾添加内容
String str="hello";  //在文件末尾所添加的内容。
out.write(str);
out.close();
}
}


4、经典题型,文件的复制(简单版本)
import java.io.*;
public class copy
{
public static void main(String args[])throws Exception{
File a=new File(args[0]);
File b=new File(args[1]);      //从cmd中输入在 运行程序时,java copy a.txt 文件路径要完整。
InputStream in=new FileInputStream(a);
byte c[]=new byte[(int)a.length()];
in.read(c);
in.close();
OutputStream out=new FileOutputStream(b);
out.write(c);
out.close();
}
}


5、打印流、System对IO的支持,输入输出的重定向。

打印流在IO中是最重要的,要重点掌握。

printStream是OutputStream的子类。简化了OutputStream的操作流程。

import java.io.*;
public class  system2
{
public static void main(String args[])throws Exception{
PrintStream pu=null;
pu=new PrintStream(new FileOutputStream("d:"+File.separator+"a.txt"));//把FileOutputStream包装进printStream 此时就简化了许多。
pu.println("yu");
pu.print("123+123");
pu.println("1231");
}
}
关于out与err的区别,这两者的结果使用情况来说,结果是一样的。只是out是输出到用户的屏幕上,而err则是不希望用户看到的。这只是一种概念上的区别。

out的好处是可以通过重定向来把错误输出到文件中来形成日志文档。如下:

public class system3
{
public static void main(String args[]){
String str="hello";
try{
System.out.println(Integer.parseInt(str));
}
catch(Exception e){
System.err.println(e);//这里吧err改成out效果是一样的。
}

}
}
in是关于输入流,是InputStream的对象。但是,这样做有一个坏处,输入的长度不确定时,中文输出会乱码。要使用BufferedReader类来实现。其标准的例子如下:

import java.io.InputStream;
public class systemin
{
public static void main(String args[])throws Exception{
InputStream in=System.in;
byte b[]=new byte[1024];  //定义输入内容的长度。
System.out.println("请输入内容");
int len=in.read(b); //开始接受数据
System.out.println("输入的内容为:"+new String(b,0,len));
in.close();

}
}
关于重定向,是重定向就是把原来要输出输入的方向改变。比较重要的一个就是把输出重定向来进行错误日志的保存,代码如下:

import java.io.*;
public class system1
{
public static void main(String args[])
{

System.setOut(new printStream(new FileOutStream("d:"+File.separator+"re.txt")));//print的重定向。不输出到屏幕上,输出到文件里。
System.out.print("abcdefg");
System.out.println("123456");

}
}
6、BufferedReader类

此为在缓冲区中对数据进行操作。是Reader的子类,可以比较好的支持中文。

import java.io.*;
public class BufferReader{
public static void main(String args[]){
BufferedReader h=new BufferedReader(new InputStreamReader(System.in));
String str=null;
System.out.println("请输入内容");
try{
str=h.readLine();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("输入的内容为"+str);
}
}
7、Scanner类..

此类较容易理解,专门用来进行输入数据的操作的。并且可以进行验证。

此类指的注意的地方就是next()和nextLine();next中输入出现了空格就重新算一个字符串,而Line是按照行来的。比如一个统计输入的字符串中含有的字母,数字,空格的一个程序。

import java.util.*;
public class chaxun
{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int a=0;
int b=0;
int c=0;
System.out.println("请输入字符串:");
String str=in.nextLine();  //忽略各种的间隔符,只以enter为结尾,本人最喜欢的一个方法。
char ch[]=str.toCharArray();
for(int i=0;i<ch.length;i++){
if(Character.isLetter(ch[i]))
{
a++;
}
if(Character.isDigit(ch[i])){
b++;
}
if(Character.isSpaceChar(ch[i])){  //Character中有关于各种字符的判定操作。详情看技术文档。
c++;
}
}
System.out.println("字母的数字为"+a);
System.out.println("数字的数字为"+b);
System.out.println("空格的数字为"+c);
}
}
还支持数字的输入和判断:如下;
import java.util.*;
public class scanner
{
public static void main(String args[]){
Scanner hu=new Scanner(System.in);
int a=0;
float b=0.0f;
System.out.print("请输入整数:");
if(hu.hasNextInt()){
a= hu.nextInt();
System.out.println("输入的整数为:"+a);
}
else{
System.out.println("输入的不是整数");
}
System.out.print("请输入小数");
if(hu.hasNextFloat()){ //判断是否小数
b= hu.nextFloat();
System.out.println("输入的小数为:"+b);
}
else{
System.out.println("输入的不是小数");
}
}
}
8、对象序列化。

此可以把对象输出到文件当中,但是是以二进制来进行输出的,对应的还有反序列化,操作大同小异。代码如下:

import java.io.*;
class Person implements Serializable
{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return "姓名"+this.name+"姓名:"+this.age;//定义Person类
}

}
public class seri
{
public static void main(String args[])throws Exception{
File f=new File("d:"+File.separator+"hu.txt");
ObjectOutputStream os=null;           //创建序列化对象。
OutputStream out=new FileOutputStream(f);
os=new ObjectOutputStream(out);
os.writeObject(new Person("李四",22));
os.close();
}
}

另外,在选择Serializable和Externalizable来进行实际的操作时的区别在于:1、Ser是系统自带的,而Ex的实现较为复杂,需要开发人员自己来完成。2、Ser全部对象都保存,而Ex可以选择哪些对象进行保存。执行效率有一定的影响3、Ser占用的空间较大,而Ex所占用的空间减少了些。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: