I/O流与网络编程
将文件通过流方式输出到控制台上
import org. junit. Test ;
import java. io. File ;
import java. io. FileReader ;
import java. io. IOException ;
public class FileTest {
@Test
public void test ( ) {
FileReader fileReader= null ;
try {
File file = new File ( "hello.txt" ) ;
fileReader = new FileReader ( file) ;
int data;
while ( ( data= fileReader. read ( ) ) != - 1 ) {
System . out. print ( ( char ) data) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
try {
if ( fileReader!= null ) {
fileReader. close ( ) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
使用read(char[] cbuf)读数据
@Test
public void test1 ( ) {
FileReader fr = null ;
try {
File file = new File ( "hello.txt" ) ;
fr = new FileReader ( file) ;
int len;
char [ ] cbuf= new char [ 5 ] ;
while ( ( len= fr. read ( cbuf) ) != - 1 ) {
String s = new String ( cbuf, 0 , len) ;
System . out. print ( s) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
try {
fr. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
写出数据到硬盘上
@Test
public void test2 ( ) {
FileWriter fileWriter = null ;
try {
File file = new File ( "hello1.txt" ) ;
fileWriter = new FileWriter ( file, true ) ;
fileWriter. write ( "I have a dream\n" ) ;
fileWriter. write ( "You need to have a dream" ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fileWriter!= null ) {
try {
fileWriter. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
文件的读入和写出
@Test
public void test3 ( ) {
FileReader fr= null ;
FileWriter fw= null ;
try {
File file = new File ( "hello1.txt" ) ;
fr = new FileReader ( file) ;
fw = new FileWriter ( "hello2.txt" ) ;
char [ ] cbuf= new char [ 5 ] ;
int len;
while ( ( len= fr. read ( cbuf) ) != - 1 ) {
fw. write ( cbuf, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fr!= null ) {
try {
fr. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fw!= null ) {
try {
fw. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
图片的复制
@Test
public void test4 ( ) {
FileInputStream fileInputStream = null ;
FileOutputStream fileOutputStream= null ;
try {
fileInputStream = new FileInputStream ( new File ( "MP130S.png" ) ) ;
fileOutputStream = new FileOutputStream ( new File ( "MP130S1.png" ) ) ;
byte [ ] buff= new byte [ 1024 ] ;
int len;
while ( ( len= fileInputStream. read ( buff) ) != - 1 ) {
fileOutputStream. write ( buff, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fileInputStream!= null ) {
try {
fileInputStream. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fileOutputStream!= null ) {
try {
fileOutputStream. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
public void testFile ( String srcPath, String desPath) {
FileInputStream fileInputStream = null ;
FileOutputStream fileOutputStream = null ;
try {
fileInputStream = new FileInputStream ( new File ( srcPath) ) ;
fileOutputStream = new FileOutputStream ( new File ( desPath) ) ;
byte [ ] buff = new byte [ 1024 ] ;
int len;
while ( ( len = fileInputStream. read ( buff) ) != - 1 ) {
fileOutputStream. write ( buff, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fileInputStream != null ) {
try {
fileInputStream. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fileOutputStream != null ) {
try {
fileOutputStream. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
使用缓冲流对非文本文件的复制
@Test
public void test6 ( ) {
FileInputStream fis = null ;
FileOutputStream fos = null ;
BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
try {
fis = new FileInputStream ( new File ( "C:\\Users\\CZ\\Desktop\\968cad867de68c74ce135336a947c3f2.mp4" ) ) ;
fos = new FileOutputStream ( new File ( "968cad867de68c74ce135336a947c3f21.mp4" ) ) ;
bis = new BufferedInputStream ( fis) ;
bos = new BufferedOutputStream ( fos) ;
byte [ ] bytes= new byte [ 1024 ] ;
int len;
while ( ( len= bis. read ( bytes) ) != - 1 ) {
bos. write ( bytes, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( bis!= null ) {
try {
bis. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( bos!= null ) {
try {
bos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fis!= null ) {
try {
fis. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fos!= null ) {
try {
fos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
使用缓冲流对文本文件的复制
@Test
public void test7 ( ) {
BufferedReader br = null ;
BufferedWriter bw = null ;
try {
br = new BufferedReader ( new FileReader ( new File ( "hello1.txt" ) ) ) ;
bw = new BufferedWriter ( new FileWriter ( new File ( "hello4.txt" ) ) ) ;
char [ ] cbuf= new char [ 5 ] ;
int len;
while ( ( len= br. read ( cbuf) ) != - 1 ) {
bw. write ( cbuf, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( br!= null ) {
try {
br. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( bw!= null ) {
try {
bw. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
对图片进行加密和解密
@Test
public void test8 ( ) {
FileInputStream fis = null ;
FileOutputStream fos = null ;
try {
fis = new FileInputStream ( new File ( "MP130S.png" ) ) ;
fos = new FileOutputStream ( new File ( "MP130S3.png" ) ) ;
byte [ ] bytes= new byte [ 1024 ] ;
int len;
while ( ( len= fis. read ( bytes) ) != - 1 ) {
for ( int i = 0 ; i < len; i++ ) {
bytes[ i] = ( byte ) ( bytes[ i] ^ 5 ) ;
}
fos. write ( bytes, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fis!= null ) {
try {
fis. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fos!= null ) {
try {
fos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
对每个字符出现的次数进行计数
@Test
public void test1 ( ) {
FileReader fr = null ;
FileWriter fw= null ;
try {
fr = new FileReader ( new File ( "zj.txt" ) ) ;
Map < Character , Integer > map= new HashMap < > ( ) ;
int len;
while ( ( len= fr. read ( ) ) != - 1 ) {
char c = ( char ) len;
if ( map. get ( c) == null ) {
map. put ( c, 1 ) ;
} else {
map. put ( c, map. get ( c) + 1 ) ;
}
}
fw = new FileWriter ( new File ( "zj1.txt" ) ) ;
Set < Map. Entry < Character , Integer > > entries = map. entrySet ( ) ;
for ( Map. Entry < Character , Integer > entry1: entries) {
switch ( entry1. getKey ( ) ) {
case ' ' :
fw. write ( "空格=" + entry1. getValue ( ) ) ;
break ;
case '\t' :
fw. write ( "tab键=" + entry1. getValue ( ) ) ;
break ;
case '\r' :
fw. write ( "回车=" + entry1. getValue ( ) ) ;
break ;
case '\n'
: fw. write ( "换行=" + entry1. getValue ( ) ) ;
break ;
default :
fw. write ( entry1. getKey ( ) + "=" + entry1. getValue ( ) ) ;
break ;
}
fw. write ( "\n" ) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fr!= null ) {
try {
fr. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fw!= null ) {
try {
fw. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
标准输入输出流的练习
import java. io. BufferedReader ;
import java. io. IOException ;
import java. io. InputStreamReader ;
public class FileTest3 {
public static void main ( String [ ] args) {
InputStreamReader isr = null ;
BufferedReader br= null ;
try {
isr = new InputStreamReader ( System . in) ;
br = new BufferedReader ( isr) ;
while ( true ) {
System . out. println ( "请输入字符串" ) ;
String data = br. readLine ( ) ;
if ( "e" . equalsIgnoreCase ( data) || "exit" . equalsIgnoreCase ( data) ) {
System . out. println ( "程序结束" ) ;
break ;
}
String s = data. toUpperCase ( ) ;
System . out. println ( s) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( br!= null ) {
try {
br. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( isr!= null ) {
try {
isr. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
}
网络编程
客户端发送信息给客户端,服务端显示信息在控制台
import org. junit. Test ;
import java. io. ByteArrayOutputStream ;
import java. io. IOException ;
import java. io. InputStream ;
import java. io. OutputStream ;
import java. net. InetAddress ;
import java. net. ServerSocket ;
import java. net. Socket ;
public class TCPTest {
@Test
public void client ( ) {
Socket socket= null ;
OutputStream os = null ;
try {
InetAddress ip= InetAddress . getByName ( "127.0.0.1" ) ;
socket = new Socket ( ip, 8899 ) ;
os = socket. getOutputStream ( ) ;
os. write ( "你好,我是客户端,我正在给你发信息" . getBytes ( ) ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( os!= null ) {
try {
os. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( socket!= null ) {
try {
socket. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
@Test
public void server ( ) {
ServerSocket ss= null ;
InputStream is = null ;
Socket socket= null ;
ByteArrayOutputStream baos= null ;
try {
ss = new ServerSocket ( 8899 ) ;
socket = ss. accept ( ) ;
is = socket. getInputStream ( ) ;
baos = new ByteArrayOutputStream ( ) ;
byte [ ] bytes= new byte [ 5 ] ;
int len;
while ( ( len= is. read ( bytes) ) != - 1 ) {
baos. write ( bytes, 0 , len) ;
}
System . out. println ( baos. toString ( ) ) ;
System . out. println ( "接收信息的ip地址为:" + socket. getInetAddress ( ) . getHostAddress ( ) ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( baos!= null ) {
try {
baos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( is!= null ) {
try {
is. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( socket!= null ) {
try {
socket. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( ss!= null ) {
try {
ss. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
}
客户端发送文件给服务器端,服务器端存在本地
import org. junit. Test ;
import java. io. * ;
import java. net. InetAddress ;
import java. net. ServerSocket ;
import java. net. Socket ;
public class TCPTest1 {
@Test
public void client ( ) {
Socket socket= null ;
OutputStream os = null ;
FileInputStream fis= null ;
try {
socket = new Socket ( InetAddress . getByName ( "127.0.0.1" ) , 9090 ) ;
os = socket. getOutputStream ( ) ;
fis = new FileInputStream ( "MP130S.png" ) ;
byte [ ] bytes= new byte [ 1024 ] ;
int len;
while ( ( len= fis. read ( bytes) ) != - 1 ) {
os. write ( bytes, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fis!= null ) {
try {
fis. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( os!= null ) {
try {
os. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( socket!= null ) {
try {
socket. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
@Test
public void server ( ) {
ServerSocket ss = null ;
Socket socket = null ;
InputStream is = null ;
FileOutputStream fos= null ;
try {
ss = new ServerSocket ( 9090 ) ;
socket = ss. accept ( ) ;
is = socket. getInputStream ( ) ;
fos = new FileOutputStream ( "MP130S1.png" ) ;
byte [ ] bytes= new byte [ 1024 ] ;
int len;
while ( ( len= is. read ( bytes) ) != - 1 ) {
fos. write ( bytes, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( fos!= null ) {
try {
fos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( is!= null ) {
try {
is. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( socket!= null ) {
try {
socket. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( ss!= null ) {
try {
ss. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
}
客户端发送文件给服务器端,服务器端存在本地,客户端再发送数据给服务器端,服务器端接收并输出
import org. junit. Test ;
import java. io. * ;
import java. net. InetAddress ;
import java. net. ServerSocket ;
import java. net. Socket ;
public class TCPTest2 {
@Test
public void client ( ) {
Socket socket = null ;
OutputStream os = null ;
FileInputStream fis= null ;
InputStream is= null ;
ByteArrayOutputStream baos= null ;
try {
socket = new Socket ( InetAddress . getByName ( "127.0.0.1" ) , 8999 ) ;
os = socket. getOutputStream ( ) ;
fis = new FileInputStream ( "MP130S.png" ) ;
byte [ ] bytes= new byte [ 10 ] ;
int len;
while ( ( len= fis. read ( bytes) ) != - 1 ) {
os. write ( bytes, 0 , len) ;
}
socket. shutdownOutput ( ) ;
is = socket. getInputStream ( ) ;
baos= new ByteArrayOutputStream ( ) ;
byte [ ] bytes1= new byte [ 10 ] ;
int len1;
while ( ( len1= is. read ( bytes1) ) != - 1 ) {
baos. write ( bytes1, 0 , len1) ;
}
System . out. println ( baos. toString ( ) ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( is!= null ) {
try {
is. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( baos!= null ) {
try {
baos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fis!= null ) {
try {
fis. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( os!= null ) {
try {
os. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( socket!= null ) {
try {
socket. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
@Test
public void server ( ) {
ServerSocket ss = null ;
Socket socket = null ;
InputStream is = null ;
FileOutputStream fos= null ;
OutputStream os = null ;
try {
ss = new ServerSocket ( 8999 ) ;
socket = ss. accept ( ) ;
is = socket. getInputStream ( ) ;
fos = new FileOutputStream ( "MP130S2.png" ) ;
byte [ ] bytes= new byte [ 50 ] ;
int len;
while ( ( len= is. read ( bytes) ) != - 1 ) {
fos. write ( bytes, 0 , len) ;
}
os = socket. getOutputStream ( ) ;
os. write ( "客户端,我这边已经收到你传输的数据" . getBytes ( ) ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( os!= null ) {
try {
os. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( fos!= null ) {
try {
fos. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( is!= null ) {
try {
is. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( socket!= null ) {
try {
socket. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( ss!= null ) {
try {
ss. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
}