例子1
package com.atguigu.java3;
import org.junit.Test;
import java.io.File;
public class FileTest {
@Test
public void test1(){
File file1 = new File("hello.txt");
System.out.println(file1);
File file2 = new File("D:\\workidea\\JavaSenior\\he.txt");
System.out.println(file2);
File file3 = new File("D:\\workidea","JavaSenior");
System.out.println(file3);
File file4 = new File(file3,"hi.txt");
System.out.println(file4);
}
}
例子10
package com.atguigu.java3;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReaderTest {
@Test
public void test1() throws Exception{
FileInputStream fis = new FileInputStream("hello.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char[] cbuf = new char[20];
int len;
while((len=isr.read(cbuf))!=-1){
String str = new String(cbuf, 0, len);
System.out.println(str);
}
isr.close();
}
}
例子11
package com.atguigu.java3;
import org.junit.Test;
import java.io.*;
public class InputStreamReaderTest {
@Test
public void test2() throws Exception {
File file1 = new File("hello.txt");
File file2 = new File("hello_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf))!=-1){
osw.write(cbuf,0,len);
}
isr.close();
osw.close();
}
}
例子12
person
package com.atguigu.java3;
import java.io.Serializable;
public class Person implements Serializable {
public static final long serialVersionUID = 34432312324L;
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
ObjectInputOutputStreamTest
package com.atguigu.java3;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectInputOutputStreamTest {
@Test
public void testObjectOutputStream() throws Exception{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("我爱北京天安门"));
oos.flush();
oos.writeObject(new Person("网名",23));
oos.flush();
oos.close();
}
@Test
public void testObjectInputStream() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
Person p = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
ois.close();
System.out.println(str);
}
}
例子13
package com.atguigu.java3;
import org.junit.Test;
import java.io.File;
import java.io.RandomAccessFile;
public class RandomAccessFileTest {
@Test
public void test1() throws Exception{
RandomAccessFile raf1 = new RandomAccessFile(new File("img.png"),"r");
RandomAccessFile raf2 = new RandomAccessFile(new File("img2.png"),"rw");
byte[] buffer = new byte[1024];
int len;
while((len=raf1.read(buffer))!=-1){
raf2.write(buffer,0,len);
}
raf1.close();
raf2.close();
}
@Test
public void test2() throws Exception{
RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
raf1.write("xyz".getBytes());
raf1.close();
}
}
例子14
package com.atguigu.java3;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
public static void main(String[] args) {
try {
InetAddress inet1 = InetAddress.getByName("192.168.10.14");
System.out.println(inet1);
InetAddress inet2 = InetAddress.getByName("www.atguigu.com");
System.out.println(inet2);
InetAddress inet3 = InetAddress.getByName("127.0.0.1");
System.out.println(inet3);
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet4);
System.out.println(inet2.getHostName());
System.out.println(inet2.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
例子15
package com.atguigu.java3;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class TCPTest1 {
@Test
public void client() throws Exception{
InetAddress inet = InetAddress.getByName("127.0.0.1");
Socket socket = new Socket(inet, 8899);
OutputStream os = socket.getOutputStream();
os.write("你好,我是客户端mm".getBytes(StandardCharsets.UTF_8));
os.close();
socket.close();
}
@Test
public void server() throws Exception{
ServerSocket ss = new ServerSocket(8899);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
ByteArrayOutputStream 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());
baos.close();
is.close();
socket.close();
ss.close();
}
}
例子16
package com.atguigu.java3;
import org.junit.Test;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
public class UDPTest {
@Test
public void sender() throws Exception{
DatagramSocket socket = new DatagramSocket();
String str = "我是UDP方式发送的导弹";
byte[] data = str.getBytes(StandardCharsets.UTF_8);
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
socket.send(packet);
socket.close();
}
@Test
public void receiver() throws Exception{
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
例子17
package com.atguigu.java3;
import java.net.URL;
public class URLTest {
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
}
}