标签:
------ <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.comtarget="blank">
java培训</a>、期待与您交流! ---------
public class FileStreams {
public static void main(String[] args) throws Exception
{
med1();
}
private static void med1() throws Exception
{
//
FileOutputStream fos = new FileOutputStream("E:\\myjava.txt");
fos.write("abcdef".getBytes());
fos.close();
}
}
private static void med3()throws Exception
{
// TODO Auto-generated method stub
// 读取一个文件中的内容
FileInputStream fis = new FileInputStream("E:\\BufferDemos.txt");
int ch = 0;
//
定义一个数组
byte[] arr=new byte[1024];
while((ch=fis.read(arr))!=-1)
{
System.out.print(new String(arr,0,ch));
}
fis.close();
}
第三种方式
private static void med4()throws Exception
{
//
// 读取一个文件中的内容
FileInputStream fis = new FileInputStream("E:\\BufferDemos.txt");
int number = fis.available();
System.out.println(number);//70 获取文章内容的长度
//定义指定内存长度的数组
byte[] buf = new byte[fis.available()];
//将内容读入数组中
fis.read(buf);
//在控制台上输出内容
System.out.println(new String(buf));
}
private static void med5() throws Exception
{
// 获取一个图片内容
FileInputStream fis = new FileInputStream("E:\\10.jpg");
//定义缓冲区
byte[] arr = new byte[fis.available()];
fis.read(arr);
// 拷贝到指定目录下
FileOutputStream fos = new FileOutputStream("E:\\5555.jpg");
fos.write(arr);
fis.close();
fos.close();
}
private static void med6() {
// TODO Auto-generated method stub
// 获取一个图片内容
FileInputStream fis;
FileOutputStream fos;
try
{
fis = new FileInputStream("E:\\10.jpg");
//定义缓冲区
byte[] arr = new byte[fis.available()];
fis.read(arr);
// 拷贝到指定目录下
fos = new FileOutputStream("E:\\5555.jpg");
fos.write(arr);
}
catch(IOException e)
{
System.out.println(e.toString());
}
finally
{
try{
if(fis!=null)
fis.close();
}
catch(IOException e)
{
System.out.println("读入失败");
}
try{
if(fos!=null)
fos.close();
}
catch(IOException e)
{
System.out.println("写入失败");
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class FileStreams {
public static void main(String[] args) throws Exception
{
med7();
}
private static void med7() throws Exception
{
//读入mp3数据
FileInputStream fis = new FileInputStream("E:\\梦中蝴蝶.mp3");
BufferedInputStream buis = new BufferedInputStream(fis);
//创建写入mp3数据的位置
FileOutputStream fos = new FileOutputStream("E:\\梦中蝴蝶01.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//建立缓冲区
byte[] arr = new byte[1024];
int number = 0;
while((number=buis.read(arr))!=-1)
{
bos.write(arr);
}
buis.close();
bos.close();
}
读取键盘录入
System.out 对应的是标准的输出设备---控制台
System.in 对应的是标准的输入设备---键盘
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Readin {
public static void main(String[] args) throws Exception{
med2();
}
private static void med2() throws Exception{
// 获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,使用转换流 InputStreamReader
InputStreamReader isr = new InputStreamReader(in);
//为了提高效率,将字符串进行缓冲区技术高效操作,使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
String line = null;
while((line = bufr.readLine())!=null)
{
if("over".equals(line))
break;
System.out.println(line.toUpperCase());
}
bufr.close();
}
// 要求输入的是over 时停止录入
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);
}
in.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Readin {
public static void main(String[] args) throws Exception{
med3();
}
private static void med3() throws Exception{
// 获取键盘录入对象
InputStream in = System.in;
//将字节流对象转成字符流对象,使用转换流 InputStreamReader
InputStreamReader isr = new InputStreamReader(in);
//为了提高效率,将字符串进行缓冲区技术高效操作,使用BufferedReader
BufferedReader bufr = new BufferedReader(isr);
OutputStream out = System.out;
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bwr = new BufferedWriter(osw);
String line = null;
while((line = bufr.readLine())!=null)
{
if("over".equals(line))
break;
bwr.write(line.toUpperCase());
bwr.newLine();
bwr.flush();
}
bufr.close();
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Readin {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
med4();
}
private static void med4()throws Exception {
// 键盘录入最常见的写法
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//将获取的数据录入到指定文件中去
BufferedWriter bwr =
new BufferedWriter(new OutputStreamWriter(newFileOutputStream("E:\\out.txt")));
String line = null;
while((line = bufr.readLine())!=null)
{
if("over".equals(line))
break;
bwr.write(line.toUpperCase());
bwr.newLine();
bwr.flush();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class Readin {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
med5();
}
private static void med5()throws Exception {
// 读取文件中数据输出到控制台上
// 键盘录入最常见的写法
BufferedReader bufr =
new BufferedReader(new InputStreamReader(new FileInputStream("E:\\IoDemos.java")));
//将获取的数据录入到指定文件中去
BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(System.out));
String line = null;
while((line = bufr.readLine())!=null)
{
if("over".equals(line))
break;
bwr.write(line.toUpperCase());
bwr.newLine();
bwr.flush();
}
}
注:流操作的基本规律
<1> 明确源和目的
源:输入流 InputStream Reader
目的: OutputStream Writer
<2>操作的数据是否是纯文本
是: 字符流
不是: 字节流
<3> 当体系明确后,来确定要具体使用哪个具体的对象
<4> 只有转换流可以指定编码表
转换流是字符与字节的桥梁
标签:
原文地址:http://blog.csdn.net/zl18603543572/article/details/46406423