----------------------
字节流基类:
InputStream(读) OutputStream(写)
(1):
public static void readFile() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
byte[] buff = new byte[1024];
int len = 0;
while ((len = fis.read(buff)) != -1) {
System.out.print(new String(buff, 0, len));
}
fis.close();
}
(2):
public static void readFile_1() throws IOException {
FileInputStream fis = new FileInputStream("demo.txt");
byte[] buff = new byte[fis.available()];
fis.read(buff);
System.out.print(new String(buff));
fis.close();
}
拷贝图片:
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("c:\\1.png");
fos = new FileOutputStream("c:\\2.png");
byte buf[] = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (IOException e) {
throw new RuntimeException("复制失败");
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
throw new RuntimeException("读取关闭失败");
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
throw new RuntimeException("写入关闭失败");
}
}
字节流的缓冲区:
关键字:BufferedOutputStream
以复制MP3为例:
public static void main(String[] args) throws IOException
{
copy();
}
public static void copy()throws IOException
{
BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("c:\\1.mp3"));
BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("c:\\2.mp3"));
int by=0;
while((by=bufis.read())!=-1){
bufos.write(by);
}
bufis.close();
bufos.close();
}
读取键盘录入:
System.out 对应的标准输出设备,控制台。
System.in 对应的标准输出设备,键盘
\r 13 \n 10
public static void main(String[] args) throws IOException
{
InputStream in=System.in;
StringBuilder sb=new StringBuilder();
while(true)
{
int ch=in.read();
if(ch=='\r')
continue;
if(ch=='\n')
{
String s=sb.toString();
if("over".equals(s))
break;
System.out.println(s.toUpperCase());
sb.delete(0,sb.length());
}
else
sb.append((char)ch);
}
}
}
读取转换流:
关键字: InputStreamReader
public static void main(String[] args) throws IOException
{
// 读取转换流:
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
// 写入流转换 :
BufferedWriter bufw=new BufferedWriter(new OutputStreamWriter(System.out));
String line=null;
while((line=bufr.readLine())!=null)
{
if("No".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
File 类:
file是用来将文件或是文件夹封装成对象。 对文件的属性进行操作。
File可以对象可以作为参数传给流的构造函数。
File的常见方法:
1:创建
boolean createNewFile();
2:删除
boolean delete();
void deleteOnExit();
3:判断
boolean exists();
4:获取信息
可以用get方法获取信息
Properties 对象:
是集合中和IO技术相结合的集合容器
该对象的特点:可以用于键值对形式的配置文件
管道流:
PipedInputStream 和 PipeedOutputStream
----------------------