81,122
社区成员




public static void main(String[] args) {
File fromFile = new File("a.txt");
try {
myDecoratorInputStream mis = new myDecoratorInputStream(new BufferedReader(new FileReader(fromFile)));
String temp = null;
StringBuffer result = new StringBuffer();
while ((temp = mis.readLine()) != null) {
result.append(temp).append("\r\n");
}
mis.close();
System.out.println(result);
} catch (Exception e) {
}
}
private static class myDecoratorInputStream extends FilterReader {
int num = 1;
protected myDecoratorInputStream(Reader in) {
super(in);
}
public String readLine() throws IOException {
String line = ((BufferedReader) in).readLine();
if (line != null) {
line = num++ + ":\t" + line;
}
return line;
}
}
//server
public static void main(String[] args) {
File file = new File("UML.png");
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ServerSocket ss = new ServerSocket(8964);
Socket s = ss.accept();
System.out.println("conect success , start transfer!");
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
byte[] bytes = new byte[1024];
int len = -1;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.flush();
bos.close();
System.out.println("transfer end!");
bis.close();
s.close();
ss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// client
public static void main(String[] args) {
File file = new File("UML2.png");
try {
Socket s = new Socket(InetAddress.getByName("localhost"), 8964);
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte[] bytes = new byte[1024];
int len = -1;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.flush();
bos.close();
bis.close();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
File fromFile = new File("a.txt");
File toFile = new File("b.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(fromFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(toFile));
StringBuffer sb = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sb.append(temp).append("\r\n");
}
br.close();
String num = "((2[0-4]|[01]?\\d?)\\d|25[0-5])";
String ip = num + "\\." + num + "\\." + num + "\\." + num;
String result = sb.toString().replaceAll("(?<=" + ip + ")\\s(?=" + ip + ")", ",");
bw.write(result);
bw.flush();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
}