//给你改了一下,把读的循环去掉就行了
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);
}
}
}
解决方案 »
- 为什么用system.out.println(str) 在控制台输出的中文都是乱码?
- Java类库的下载
- javac的编译遇到的小问题
- print("//".matches("//"));我在Eclipse3.2中运行是true,但是在北京尚学堂马士兵的视频课程中,说是false,为什么?
- jdbc.drivers属性设定
- 100分问一个打包问题!
- 请教:不变模式与String
- 敢问错在何方?初学java
- 用jdbc连接oracle数据库时,关于DriverManager.getConnection(url,name,password)方法的问题
- dom4j
- 求助:用jb做可执行文件。。
- Java中有时间输入控件吗?
the test ?汯癥?潵?楳猠祯
EOF reached
如果你要读肉眼能看懂的文件,就应该用BufferedReader,写文件就应该用PrintWriter
我要做的就是把一个字符串写到文件(txtTestFile.txt),然后把他又读出来.
PrintWriter一般是格式二进制数据..BufferReader只不过是缓冲Reader类的子类的.而Reader类发险象环生不能用于读文件.
望高手们看看,问题出在那里,如果有更好解决方法,或不同的方法.给我指点一下.
为什么是筹码的原因?