I/O系统
输入流InputStream
File file = new File("E:\\ello"); //找到指定文件
if(file.exists()){//文件是否存在
System.out.println("存在");
}else{
System.out.println("不存在");
if(!file.isDirectory()){
try {
file.createNewFile();//文件不存在就新建一个次名字的文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
InputStream inputStream = new FileInputStream(file);//读取内容,InputStream 是抽象类,只能实例化继承他的子类
try {
int b = 0;
while( (b = inputStream.read()) != -1){
System.out.print((char)b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件不存在");
}
inputStream .close();//关闭流,释放内存空间
FileReader与InputStream类似,并且可以读取中文。
输出流OutputStream
OutputStream outputStream=new FileOutputStream("src/hello.txt");
outputStream.write(x);//写入Int类型的值x
FileWriter fileWriter=new FileWriter ("");
fileWriter.close();//立即写入
String str="world";
byte[] bytes=str.getBytes();
out.write(bytes);
out.flush;//清空缓存区,立即写入
//str.subString(0,str.indexOf(l)); 截取字符串str下标0到l的字符
BufferReader
readLine();//一行一行的读
Properties以键值对的方式存放数据
网络编程
TCP
ServerSocket与 Socket都在java.net包下
//服务器端
public class SeverTest {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(6669);//端口号0-65535 100之后
while(true){
Socket s = server.accept();//接收客户端的链接 阻塞式的等待
// InetAddress address = s.getInetAddress();
// System.out.print(address.getHostAddress());
while(true){
InputStream inputStream = s.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
System.out.println("客户端说:"+dataInputStream.readUTF());//readUTF 阻塞
OutputStream out = s.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
System.out.println("qingshuohua");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
dataOutputStream.writeUTF(str);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//客户端
public class SocketTest {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost",6669);
while(true){
OutputStream out = s.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
System.out.println("qingshuohua");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
dataOutputStream.writeUTF(str);
InputStream inputStream = s.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
System.out.println("服务端说:"+dataInputStream.readUTF());
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
UDP
public class SendTest {
public static void main(String[] args) {
String str="hello";
byte[] buf=str.getBytes();
try {
DatagramPacket packet=new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1",8523));
DatagramSocket socket=new DatagramSocket(4747);
try {
socket.send(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ReceiveTest {
public static void main(String[] args) {
byte[] buf=new byte[1024];
DatagramPacket paket=new DatagramPacket(buf, buf.length);
try {
DatagramSocket socket=new DatagramSocket(8523);
try {
socket.receive(paket);
System.out.println(new String(paket.getData()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
XML解析技术
XML文档由两部分组成:序言和文档元素。
XML文档声明: 由"<?xml>"开始,以"?>"结束
里面包含版本号(version)、独立文档说明(standalone)和编码声明(encoding)
目前版本号只有1.0,编码一般用UTF-8。
XML文档注释:以"<!--"开始,"-->"结束