?? filechanneltester.java
字號:
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;
public class FileChannelTester {
public static void writeFile(String address,String str)throws IOException{
final int BSIZE=1024;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String time = sdf.format(new Date()).toString();
//向文件中寫數據
//FileChannel fc=new FileOutputStream("logs.txt").getChannel();
//FileChannel fc=new FileOutputStream("logs.txt").getChannel();
// fc.write(ByteBuffer.wrap("你好,".getBytes()));
// fc.close();
File file = new File(".\\logs");
if(!file.exists())file.mkdirs();
//向文件末尾添加數據
FileChannel fc=new RandomAccessFile(".\\logs\\logs.txt","rw").getChannel();
fc.position(fc.size()); //定位到文件末尾
if(null != str && !"".equals(str)){
str = time + ":" + address + ",請求的頁面為 " + str + " 。\r\n\r\n";
} else {
str = time + ":" + address +" 。\r\n\r\n";
}
fc.write(ByteBuffer.wrap(str.getBytes()));
fc.close();
//讀數據
//fc=new FileInputStream("D:\\logs.txt").getChannel();
fc=new FileInputStream(".\\logs\\logs.txt").getChannel();
ByteBuffer buff=ByteBuffer.allocate(BSIZE);
fc.read(buff); //把文件中的數據讀入到ByteBuffer中
buff.flip();
Charset cs=Charset.defaultCharset(); //獲得本地平臺的字符編碼
String str2 = cs.decode(buff).toString(); //轉換為Unicode編碼
System.out.println(str2);
fc.close();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -