//给你改了一下,把读的循环去掉就行了
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;public class FileRWTest
{
public static void main(String[]str)
{
String str1 = "I love you miss you";
File txtFile = new File("txtTestFile.txt");
ByteBuffer buf = ByteBuffer.allocate(100);
FileOutputStream outFile = null;
FileInputStream inFile = null;
try
{
txtFile.createNewFile();
outFile = new FileOutputStream(txtFile);
inFile = new FileInputStream(txtFile);
}
catch (FileNotFoundException e)
{
e.printStackTrace(System.err);
}
catch (IOException e)
{
e.printStackTrace(System.err);
}
FileChannel outChannel = outFile.getChannel();
FileChannel inChannel = inFile.getChannel();
buf.put(str1.getBytes());
buf.flip();
try
{
outChannel.write(buf);
buf.clear();
outFile.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(0);
} try
{
inChannel.read(buf);
System.out.println(" the test " +(((ByteBuffer)buf.flip()).asCharBuffer().toString()));
buf.clear();
System.out.println("EOF reached");
inFile.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(0);
}
}
}
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;public class FileRWTest
{
public static void main(String[]str)
{
String str1 = "I love you miss you";
File txtFile = new File("txtTestFile.txt");
ByteBuffer buf = ByteBuffer.allocate(100);
FileOutputStream outFile = null;
FileInputStream inFile = null;
try
{
txtFile.createNewFile();
outFile = new FileOutputStream(txtFile);
inFile = new FileInputStream(txtFile);
}
catch (FileNotFoundException e)
{
e.printStackTrace(System.err);
}
catch (IOException e)
{
e.printStackTrace(System.err);
}
FileChannel outChannel = outFile.getChannel();
FileChannel inChannel = inFile.getChannel();
buf.put(str1.getBytes());
buf.flip();
try
{
outChannel.write(buf);
buf.clear();
outFile.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(0);
} try
{
inChannel.read(buf);
System.out.println(" the test " +(((ByteBuffer)buf.flip()).asCharBuffer().toString()));
buf.clear();
System.out.println("EOF reached");
inFile.close();
}
catch (IOException e)
{
e.printStackTrace(System.err);
System.exit(0);
}
}
}
the test ?汯癥?潵?楳猠祯
EOF reached
如果你要读肉眼能看懂的文件,就应该用BufferedReader,写文件就应该用PrintWriter
我要做的就是把一个字符串写到文件(txtTestFile.txt),然后把他又读出来.
PrintWriter一般是格式二进制数据..BufferReader只不过是缓冲Reader类的子类的.而Reader类发险象环生不能用于读文件.
望高手们看看,问题出在那里,如果有更好解决方法,或不同的方法.给我指点一下.
为什么是筹码的原因?