目录
IO流
输入/输出流
“流”是一个抽象概念,是指不同设备间数据传输内容的抽象。当需要从一个数据源读取或是向一个目标写入一个数据时,就可以使用流。数据源可以是文件、内存、网络连接等,流就是这些数据砸死传输过程中的抽象概念,也可以理解为一个有序列的数据。
输入流
InputStream类
用于处理字节流,用于处理汉字时可能会出现乱码。
方法 | 功能描述 |
---|---|
available() | 返回输入流预估的可读字节数 |
close() | 关闭输入流并释放系统资源 |
mark(int readlimit) | 标记输入流中的当前位置 |
markSupported() | 判断输入流是否支持mark()和reset()方法 |
read() | 从输入流中读取下一个字节的数据 |
reset() | 将输入流重新定位到上次在此输入流调用mark()方法的位置 |
skip(long n) | 跳过并丢弃输入流n个字节数据 |
FileInputStream fis=null;
try {
fis = new FileInputStream("文件地址");
byte[] butter=new byte[5];
int len;
while ((len=fis.read(butter))!=-1){
String str=new String(butter,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis==null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream中的所有方法调用时都会抛出IOException。建议养成使用try-catch而不是throw。
Reader类
与InputStream类相对,此类方法是用于处理字节流的
File file=new File("文件地址");
FileReader fr=null;
try {
fr=new FileReader(file);
char[] cbuf=new char[5];
int len;
while ((len=fr.read(cbuf))!=-1){
String str=new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr==null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出流
OutputStream类
与InputStream相对。
Writer类
与Reader类相对。
输入输出流的一些举例:
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream("D:\\尚硅谷\\第一章 Java基础\\ProjtctTest\\src\\FileInOutputTest\\1.抛抓模型.png");
fos=new FileOutputStream("D:\\尚硅谷\\第一章 Java基础\\ProjtctTest\\src\\FileInOutputTest\\1.抛抓模型1.png");
byte[] buffer=new byte[5];
int len;
while ((len= fis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
下面是buffer的测试
FileInputStream fis=null;
FileOutputStream fos=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
fis=new FileInputStream("D:\\尚硅谷\\第一章 Java基础\\ProjtctTest\\src\\FileInOutputTest\\1.抛抓模型.png");
fos=new FileOutputStream("D:\\尚硅谷\\第一章 Java基础\\ProjtctTest\\src\\FileInOutputTest\\1.抛抓模型2.png");
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
byte[] buffer=new byte[5];
int len;
while ((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos!=null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis!=null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long start=System.currentTimeMillis();
String scrPath="C:\\Users\\蓝天\\Desktop\\01-尚硅谷-Java语言基础-资料文件结构1.avi";
String destPath="C:\\Users\\蓝天\\Desktop\\复印件2.avi";
copyfilewithbuffered(scrPath,destPath);
long end=System.currentTimeMillis();
System.out.println("复制花费的时间为:"+(end-start));
}
public static void copyfilewithbuffered(String scrFile,String destFile){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
FileInputStream fis = new FileInputStream(scrFile);
FileOutputStream fos = new FileOutputStream(destFile);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
byte[] buffer=new byte[1024];
int len;
while ((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos!=null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis!=null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对象流的整理
/**
* 对象流的使用
* ObjectInputStream和ObjectOutputStream的使用
*2.作用:用于存取和读取基本数据类型数据或对象的处理流
*3.要想一个类可序列化,需要满足以下要求:
* (1).继承Serializable接口(标识接口)
* (2).要自定义一个SerialVersionUID
*/
public class FileInputOutput {
/*
序列化过程:将内存中的Java对象保存到盘中或通过网络传输出去
使用ObjectOutputStream
*/
public static void main(String[] args) {
File file=new File("D:\\尚硅谷\\第一章 Java基础\\ProjtctTest\\src\\FileInputOutputStreamTest\\Object.dat");
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject("我爱中国");
oos.flush();
oos.writeObject(new person("李明",18));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(oos!=null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转换流的整理
/**
* 1.转换流:
* InputStreamReader:将一个字节的输入流转换为一个字符的输入流
* OutputStreamWriter:将一个字符的输出流转换为一个字节的输出流
*2.作用:提供字符流与字节流之间的转换
*3.解码:字节、字节数组--->字符数组、字符串
* 编码:字符数组、字符串--->字节、字节数组
*/
public class InputStreamReaderTest {
public static void main(String[] args) {
FileInputStream fis=null;
InputStreamReader isr=null;
try {
fis=new FileInputStream("D:\\尚硅谷\\第一章 Java基础\\ProjtctTest\\src\\FileInputOutputStreamTest\\hello.txt");
isr=new InputStreamReader(fis);
char[] cbuf=new char[5];
int len;
while ((len=isr.read(cbuf))!=-1){
String str=new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr!=null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
网络编程部分
客户端
public static void main(String[] args) {
Socket socket=null;
OutputStream os=null;
try {
//1.创建socket对象,指明服务端IP和端口号
InetAddress inet=InetAddress.getByName("192.168.3.13");
socket=new Socket(inet,8899);
//2.获取一个输出流,用于输出数据
os=socket.getOutputStream();
//3.写出数据操作
os.write("你好我是客服giegie".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.资源的关闭
try {
if (os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {if (socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端
public class Serve {
public static void main(String[] args) {
ServerSocket ss=null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream baos=null;
try {
//1.创建服务器端的SeverSocket,指明自己的端口号
ss=new ServerSocket(8899);
//2.调用accept()表示接受来自客户端的Socket
socket=ss.accept();
//3.获取输入流
is=socket.getInputStream();
//4.读取输入流中的数据
baos=new ByteArrayOutputStream();
byte[] buffer=new byte[5];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
System.out.println("收到了来自:"+socket.getInetAddress().getHostAddress()+"的消息");
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.关闭资源
try {
if(baos!=null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ss!=null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
通过URL从网页直接下载文件
public static void main(String[] args) {
try {
URL url=new URL("http://121.196.98.183:8080/img/ok.txt");
//public String getProtocol() 获取URL协议名
System.out.println(url.getProtocol());
//public String getHost() 获取URL主机名
System.out.println(url.getHost());
//public String getPort() 获取URL端口号
System.out.println(url.getPort());
//public String getPath() 获取URL文件路径
System.out.println(url.getPath());
//public String getFile() 获取URL文件名
System.out.println(url.getFile());
//public String getQuery() 获取URL的查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
InputStream is=null;
FileOutputStream fos=null;
try {
URL url=new URL("http://121.196.98.183:8080/img/ok.txt");
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.connect();
is=urlConnection.getInputStream();
fos=new FileOutputStream("D:\\尚硅谷\\第一章 Java基础\\ProjtctTest\\src\\InternetTest");
byte[] buffer=new byte[1024];
int len;
while ((len=is.read())!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}