package v2ch01.textFile; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.util.Scanner; import org.junit.Test; public class FileTest { /** * 以字符的方式寫入文本 * @throws FileNotFoundException * @throws UnsupportedEncodingException */ @Test public void writeWenBen() throws FileNotFoundException, UnsupportedEncodingException { PrintWriter out = new PrintWriter(new File("e://test.txt"),"utf-8"); out.write("abcde"); // out.flush(); out.close(); } /** * 以字符的方式讀取文本 * @throws FileNotFoundException */ @Test public void readWenBen() throws FileNotFoundException{ Scanner scanner = new Scanner(new FileInputStream(new File("e://test.txt")), "utf-8"); while(scanner.hasNext()) System.out.println(scanner.nextLine()); } /** * 以字節的方式寫入文本 * @throws FileNotFoundException */ @Test public void writeByte() throws FileNotFoundException{ OutputStream outputStream = new FileOutputStream(new File("e://test.txt")); String str = "I LOVE YOU EVE "; char[] ch = str.toCharArray(); // char[] ch = {'a','b','c','d'}; for (char c : ch) { try { outputStream.write(c); } catch (IOException e) { e.printStackTrace(); } } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 以字節的方式讀取文本 */ @Test public void readByte(){ try { InputStream inputStream = new BufferedInputStream(new FileInputStream("e://test.txt")); int i=0; StringBuilder builder = new StringBuilder(); while((i=inputStream.read())!=-1){ builder.append((char)i); } System.out.println(builder.toString()); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }
java NIO從一個文件寫到另外一個文件java
/**
*通道之間的數據傳輸
* transferFrom()方法將數據源從通道傳輸到FileChannel中
* @throws IOException
*/
@Test
public void transferFrom() throws IOException{
RandomAccessFile fromFile = new RandomAccessFile("e://1.txt", "rw");
FileChannel fromChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile("e://2.txt", "rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
toChannel.transferFrom(fromChannel, position, count);
}
/**
* transferTo()方法將數據從FileChannel傳輸到其餘的channel中
* @throws IOException
*/
@Test
public void transferTo() throws IOException{
RandomAccessFile fromFile = new RandomAccessFile("e://1.txt", "rw");
FileChannel fromChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile("e://2.txt", "rw");
FileChannel toChannel = toFile.getChannel();
long position=0;
long count = fromChannel.size();
fromChannel.transferTo(position, count, toChannel);
}
app
Java NIO讀取文件(可能中文輸出亂碼)dom
/**
* FileChannel沒法設置爲非阻塞模式,老是運行在阻塞模式下
* @throws IOException
*/
@Test
public void testReadFromChannel() throws IOException{
Charset charset = Charset.forName("utf-8");
RandomAccessFile file = new RandomAccessFile(new File("e://2.txt"), "rw");
FileChannel channel = file.getChannel();
ByteBuffer buf = ByteBuffer.allocate(40000);
int byteRead = channel.read(buf);
StringBuilder sb = new StringBuilder();
while(byteRead!=-1){
buf.flip();
while(buf.hasRemaining()){
sb.append((char)buf.get());
}
buf.clear();
byteRead = channel.read(buf);
}
System.out.println(sb.toString());
}ui
Java NIO寫數據到文件spa
/**
* 向FileChannel中寫數據
* @throws IOException
*/
@Test
public void testWriteToChannel() throws IOException{
@SuppressWarnings("resource")
RandomAccessFile file = new RandomAccessFile("e://2.txt", "rw");
FileChannel channel = file.getChannel();
String newData = "New String to write to 我是阿凡達 ..... "+System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while(buf.hasRemaining()){
channel.write(buf);
}
channel.close();
}code