P234
3.建立缓冲区
public static String loadStream(InputStream in) throws IOException{
int len;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
while((len=in.read())!=-1) {
baos.write(buffer,0,len);
}
baos.close();
return baos.toString();
}
4.输入流byte转化为大写字母
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
public class test {
public static String swap(byte[] b) throws UnsupportedEncodingException {
String ans=new String(b,"utf-8");
int i;
String ans2=ans.toUpperCase();
return ans2;
}
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
Scanner input=new Scanner(System.in);
byte b[]=new byte[1024];
int cnt=System.in.read(b);
System.out.println(swap(b));
}
}
5.将文件中的内容转化为字符串:
package Multiserver;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class three {
static String loadFile(String filename) {
String content="";
try {
String path=filename;
Stream<String> lines=Files.lines(Paths.get(path));
content=lines.collect(Collectors.joining(System.lineSeparator()));
}
catch(IOException e) {
e.printStackTrace();
}
return content;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
String filename="C:\\Users\\24772\\Desktop\\临时文件\\java\\test.txt";
String ans=loadFile(filename);
System.out.println(ans);
}
}
6.将contents内容输入到filename中
package Multiserver;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class three {
static public boolean saveFile(String filename,String contents){
try {
File file=new File(filename);
PrintStream ps=new PrintStream(new FileOutputStream(file));
ps.println(contents);
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
return true;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
String filename="C:\\Users\\24772\\Desktop\\临时文件\\java\\test\\test6.txt";
Scanner input=new Scanner(System.in);
String contents=input.next();
saveFile(filename,contents);
}
}
7.使用socket.getInputStream()
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String filename="...";//文件路径
String content="";
String readline;
readline=sin.readLine();
File file=new File(filename);
PrintStream ps=new PrintStream(new FileOutputStream(file));
while(!readline.equals("quit")) {
os.println(readline);//服务器端输出
os.flush();
System.out.println("--------------------------------------");
content=is.readLine();
System.out.println("服务器:"+content);//服务器端os来的
time.timewriting();//输出当前时间
ps.println(contents);//写入文件;
readline=sin.readLine();
}
os.close();
is.close();
socket.close();
8.查找文件
package Multiserver;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class three {
static File ans;
static public boolean findFile(String filename,File dir){
boolean flg=false;
File []files=dir.listFiles();
for(File file:files) {
if(file.isDirectory()) {
findFile(filename,file.getAbsoluteFile());//getabsolutefile:以该目录名作为下次目录访问
}
if(file.isFile()&&filename.equals(file.getName())) {
flg=true;
ans=file.getAbsoluteFile();
break;
}
}
return flg;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
String filename="C:\\Users\\24772\\Desktop\\临时文件\\java\\test\\test6.txt";
Scanner input=new Scanner(System.in);
File dir=new File(input.next());//C:\\Users\\24772\\Desktop\\临时文件
boolean flg=findFile(filename,dir);
System.out.println(flg);
if(flg) {
System.out.println(ans.getName());
}
else {
System.out.println("no such file");
}
}
}
p252.6 socket通信
//服务器端
package Multiserver;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
import message.Message;
import message.MessageType;
class timewrite{
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
public String timerun(){
return dateFormat.format(date);
}
public void timewriting() {
System.out.println(dateFormat.format(date));
}
}
public class serverthread<message> extends Thread{
Socket socket=null;
int clientnum;
private DataInputStream dis;
private FileOutputStream fos;
private ObjectInputStream oIn;
private ObjectOutputStream oOut;
private Vector<serverthread> vector;
private String name;
timewrite time=new timewrite();
Scanner input=new Scanner(System.in);
public serverthread(Socket socket,int num,Vector<serverthread> vector) {
this.socket=socket;
this.clientnum=num+1;
this.vector=vector;
vector.add(this);
}
public void run() {
try {
//File directory=new File("C:\\学校\\java\\工作区域\\testnew.txt");
System.out.println("接收文件/发送消息? 1/0");
int flg=input.nextInt();
if(flg==0) {
/*String line,linefromclient;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client "+clientnum+":"+is.readLine());
line=sin.readLine();
while(true) {
os.println(line);
os.flush();
linefromclient=is.readLine();
if(linefromclient.equals("exit")) {
break;
}
//System.out.println("---------------------------------------");
System.out.println("Client "+clientnum+":"+linefromclient+" "+time.timerun());
//time.timerun();
line=sin.readLine();
}
os.close();
is.close();
socket.close();*/
System.out.println("客户端:" + socket.getInetAddress().getHostAddress() + "已连接!");
oIn=new ObjectInputStream(socket.getInputStream());
oOut=new ObjectOutputStream(socket.getOutputStream());
while(true) {
Message message=(Message)oIn.readObject();
int type=message.getType();
switch(type) {
case MessageType.TYPE_SEND:
String to=message.getTo();
System.out.println("messagefrom :"+message.getFrom()+" to :"+message.getTo());
System.out.println(message.getInfo());
serverthread ut;
int size=vector.size();
for(int i=0;i<size;i++) {
ut=vector.get(i);
if(to.equals(ut.name)&&ut!=this) {
ut.oOut.writeObject(message);
}
}
break;
case MessageType.TYPE_LOGIN:
name=message.getFrom();
message.setInfo("欢迎来到ycy聊天室!");
oOut.writeObject(message);
break;
}
}
}
else {
try {
dis=new DataInputStream(socket.getInputStream());
String filename=dis.readUTF();
long filelength=dis.readLong();
File directory=new File("C:\\学校\\java\\工作区域");
if(!directory.exists()) {
directory.mkdir();
}
File file=new File(directory.getAbsolutePath()+File.separatorChar+filename);
fos=new FileOutputStream(file);
byte[] bytes=new byte[1024];
int length=0;
while((length = dis.read(bytes, 0, bytes.length)) != -1) {
fos.write(bytes, 0, length);
fos.flush();
}
System.out.println("======== 文件接收成功 [File Name:" + filename + "] ========="+time.timerun());
//time.timerun();
System.out.println("是否查看文件内容:输入查看范围(前?个字符||输入0退出)");
int checkflg=input.nextInt();
if(checkflg==0) {
System.out.println("-----------不看算了。。。-----------");
}
else {
int length2=bytes.length;
for(int j=0;j<checkflg;j++) {
System.out.print((char)bytes[j]);
}
System.out.println();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(fos != null)
fos.close();
if(dis != null)
dis.close();
socket.close();
} catch (Exception e) {
}
}
}
}
//
//BufferedReader is2=new BufferedReader(new InputStream Reader())
//
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
}
论述题:
1.流的分类:
从流动方向上看:输入流和输出流
从读取类型上看:字节流和字符流
流发生的源头看:节点流和过滤流
2.字节流常用子类:
InputStream:
ByteArrayInputStream: 参数为一个字节数组,创建一个 ByteArrayInputStream ,使其使用 buf作为其缓冲区数组
FileInputStream:对文件以字节的方式读取。
PipedInputStream、PipedOutputStream:通常用于将一个程序的输出连接到另一个程序的输入。 输出流作为管道的发送端,输入流作为管道的接收端。
3.字节流和字符流的转化:
输入字节流转为字符流:使用inputstreamreader构造方法
输出字符流转为字节流:使用OutPutStreamWriter或者PrintWriter方法。
4.过滤流、流的装配:
过滤流: BufferedInputStream和BufferedOutputStream, 缓存作用,用于装配文件磁盘、网络设备、终端等读写开销大的节点流,提高读写性能;DataInputStream和DataOutputStream, 可从字节流中写入、读取Java基本数据类型,不依赖于机器的具体数据类型,方便存储和恢复数据
5.序列化、反序列化:
序列化(Serialization):将实现了Serializable接口的对象转换成一个字节序列,并能够在以后将这个字节序列完全恢复为原来的对象,后者又称反序列化。
序列化的目的:对象持久化( persistence );网络传输(RMI远程方法调用)
Java对序列化的支持:ObjectInputStream类和;ObjectOutputStream类
6.File
1. File类主要是JAVA为文件这块的操作(如删除、新增等)而设计的相关类
2. File类的包名是java.io,其实现了Serializable, Comparable两大接口以便于其对象可序列化和比较
7.文件读写:
file可以作为FileOutputStream/FileInputStream的参数对象,参与到读写当中