输入输出是所有程序都必需的部分,使用输入机制,允许程序读取外部数据(包括磁盘、光盘等);使用输出机制,可以将程序数据输出到磁盘中。Java主要包括输入、输出两种IO流,每种流都可以用字节流或字符流来处理数据。字节流以字节为单位来处理数据,字符流以字符为单位处理数据。
File类
File类对象主要用来获取文件本身的一些信息,创建和删除文件或文件夹以及运行可执行文件。File类不能访问文件的内容,需要输入输出流来读取。File类的构造方法有下面三个:
File(String name)
File(String diretoryPath,String filename)
File(File f,String filename)
1.获取文件的属性
文件对象的属性有很多,比如常见的有:文件的名字、是否可读、是否存在、文件的绝对路径、文件是否为一个普通文件等等。我们可以调用相应的方法来得到这些属性。先在桌面创建了一个text.txt文件,下面我用了一下File类的常用方法:
File f1=new File("C:\\Users\\22075\\Desktop\\text.txt"); // '\\'为转义字符\
File f2=new File("C:\\Users\\22075\\Desktop\\text1.txt"); //创建一个文件不存在的File对象
System.out.println(f1.exists()); //判断文件是否存在
System.out.println(f1.getName()); //得到文件的名称
System.out.println(f1.isFile()); //判断是否是普通文件
System.out.println(f2.exists());
//list()方法返回文件对象下的所有文件名
File file=new File("C:\\Users\\22075\\Desktop");
String [] fileList=file.list();
//一个数组接收后输出
for (String string : fileList) {
System.out.println(string);
}
2.文件的创建与删除
如果用File类创建的对象实例后,没有对应的文件,我们可以调用createNewFile()方法创建一个新文件,调用delete()方法可以删除File类对象所对应文件。
File f1=new File("C:\\Users\\22075\\Desktop\\text.txt");
File f2=new File("C:\\Users\\22075\\Desktop\\text1.txt");
if(f1.exists())
{
f1.delete();
}
if(!f2.exists())
{
f2.createNewFile();
}
3.运行可执行文件
得到可执行文件的绝对路径后,通过Runtime类对象的exec方法,可以执行文件。这里启动了一个QQ音乐。
Runtime rc=Runtime.getRuntime();
File f=new File("C:\\Program Files (x86)\\Tencent\\QQMusic\\QQMusic.exe");
rc.exec(f.getAbsolutePath());
IO流
输入输出流涉及一个方向问题,这里的输入输出是从运行中程序来考虑的。对于输入流:只能读取数据,不能写入数据;对于输出流:只能写入数据,不能读取数据;例如,程序将数据从内存送到硬盘,应该为输出流;如果从网络上读取一个网页,程序应该读取数据,应该为输入流。
Java的输入流主要由InputStream和Reader类作为基类,输出流主要由OutputStream和Writer作为基类。
下面我用了一个输入流的继承类FileInputStream来读取数据文件中的数据,Reader类与该类类似,不过Reader类以字符为单位读取数据。查看JDK文档,该类有以下的方法:
- 构造方法:FileInputStream(File file)
- read(byte[] b):从输入流中读取最多b.length个字节的数据,并存储在b中,返回实际读取的字节数。若返回-1,表明读取到输入流的结束点。
//创建文件对象
File file=new File("C:\\Users\\22075\\Desktop\\ysg.txt");
//构造输入流
FileInputStream inputStream=new FileInputStream(file);
//声明字节数组存储数据,长度为1024
byte[] b=new byte[1024];
//存储读取的实际长度
int hasRead=0;
while((hasRead=inputStream.read(b))!=-1){
System.out.println(new String(b,0,hasRead));
}
//关闭输入流
inputStream.close();
输出流常用的方法是write()方法,向流中输出数据。下面用输入流读取文件中的数据,输出流将类容写入一个文本文件:
//创建文件对象
File file=new File("C:\\Users\\22075\\Desktop\\ysg.txt");
//构造输入流
FileInputStream inputStream=new FileInputStream(file);
//构造输出流
FileOutputStream outputStream=new FileOutputStream("C:\\Users\\22075\\Desktop\\ysg1.txt");
//声明字节数组存储数据,长度为1024
byte[] b=new byte[1024];
//存储读取的实际长度
int hasRead=0;
while((hasRead=inputStream.read(b))!=-1){
outputStream.write(b, 0, hasRead);
}
inputStream.close();
outputStream.close();
}
字节流和字符流很相似,在大多数时候,我们会用到字节流,但是字节流不能很好地处理Unicode字符(2个字节),比如,一个汉字在文件中占用2个字节,如果用字节流读取,有可能截断该文字对应的字节流,造成乱码。
缓冲流
缓冲流Buffered和BufferedWriter是更高级的流,我们可以用缓冲流按行读取或写入数据。
//要输入的数据
String[] data={"hello,ysg","ysgncss","zhangshan","lisi"};
File file=new File("C:\\Users\\22075\\Desktop\\ysg.txt");
//字节输出流对象
FileWriter fileWriter=new FileWriter(file);
//添加缓冲
BufferedWriter wBufferedWriter=new BufferedWriter(fileWriter);
for (String string : data) {
wBufferedWriter.write(string);
//换行
wBufferedWriter.newLine();
}
wBufferedWriter.close();
fileWriter.close();
//字符输入流
FileReader reader=new FileReader(file);
BufferedReader bufferedReader=new BufferedReader(reader);
String string=null;
while((string=bufferedReader.readLine())!=null){
System.out.println(string);
}
bufferedReader.close();
reader.close();