import java.io.*;
public class TestFileWriter {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("D:\\test\\ab.txt");
for(int c=0;c<=50000;c++){
fw.write(c); //为啥写入的不是数字?
}
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);
}
}
}API 上的说明是:writepublic void write(int c)
throws IOException写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略。
如注释,为啥写入的是 int , 而输出的却不是?
public class TestFileWriter {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("D:\\test\\ab.txt");
for(int c=0;c<=50000;c++){
fw.write(c); //为啥写入的不是数字?
}
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);
}
}
}API 上的说明是:writepublic void write(int c)
throws IOException写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略。
如注释,为啥写入的是 int , 而输出的却不是?
16位的int就会和char一一对应了,所以输出的是字符了
我知道int 是32位 ,char16位,我想知道他为啥不直接些int,而要写入char
只能写入char,而不能写入int吗?
你想写入数字,可以用write(String str, int off, int len) ,比如string s="123";
synchronized (lock) {
if (writeBuffer == null){
writeBuffer = new char[writeBufferSize];
}
writeBuffer[0] = (char) c;
write(writeBuffer, 0, 1);
}
}
因为被强制转换成char了
没有直接写整数的io操作流类。。
用记事本打开应该是乱码