BufferedWriter的write用法

start3201277 2018-03-26 05:34:41
int mark=1;
fw = new FileWriter("d:/myrecorder.txt");
bw = new BufferedWriter(fw);
bw.write(mark+"");
这段代码可以输出1,但是最后一句改成bw.write(mark)就无法输出了,不是可以输出int类型吗
...全文
1110 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
start3201277 2018-03-27
  • 打赏
  • 举报
回复
引用 3 楼 u012445835 的回复:
如果是bw.write(mark+""); 就会调用
 public void write(String str) throws IOException {
        write(str, 0, str.length());
    }
也就是输出字符串,所以就是1 如果调用bw.write(mark);因为你传的值是int类型会进入这个
 public void write(int c) throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (nextChar >= nChars)
                flushBuffer();
            cb[nextChar++] = (char) c;
        }
    }
注意看如果是int 会转成char类型,此时对应 Ascii 表。比如 Ascii 表中33对应“!”。 你可以将mark设置为33 看看,输出结果就是"!"
谢谢解惑
心随念 2018-03-27
  • 打赏
  • 举报
回复
如果是bw.write(mark+""); 就会调用
 public void write(String str) throws IOException {
        write(str, 0, str.length());
    }
也就是输出字符串,所以就是1 如果调用bw.write(mark);因为你传的值是int类型会进入这个
 public void write(int c) throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (nextChar >= nChars)
                flushBuffer();
            cb[nextChar++] = (char) c;
        }
    }
注意看如果是int 会转成char类型,此时对应 Ascii 表。比如 Ascii 表中33对应“!”。 你可以将mark设置为33 看看,输出结果就是"!"
start3201277 2018-03-26
  • 打赏
  • 举报
回复
可是打开文件后什么空白的,没有显示
oyljerry 2018-03-26
  • 打赏
  • 举报
回复
int输出就是二进制的1,你打开文件查看的时候用二进制等方式查看就知道了
实验1 分析成绩单 1. 实验目的:掌握字符输入、输出流用法。 2. 实验代码: Fenxi: import java.util.*; public class Fenxi{ public static double getTotalScore(String s){ Scanner scanner=new Scanner(s); scanner.useDelimiter("[^0123456789.]+"); double totalScore=0; while(scanner.hasNext()){ try{ double score=scanner.nextDouble(); totalScore=totalScore+score; } catch(InputMismatchException exp){ String t=scanner.next(); } } return totalScore; } } AnalysisResult: import java.io.*; import java.util.*; public class AnalysisResult{ public static void main(String args[]){ File fRead=new File("score.txt"); File fWrite=new File("scoreAnalysis.txt"); try{ Writer out= new FileWriter(fWrite,true);//以尾加方式创建指向文件fWrite的out流 BufferedWriter bufferWrite=new BufferedWriter(out); //创建指向out的bufferWrite流 Reader in=new FileReader(fRead); //创建指向文件fRead的in流 BufferedReader bufferRead=new BufferedReader(in); //创建指向in的bufferRead流 String str=null; while ((str=bufferRead.readLine())!=null){ double totalScore =Fenxi.getTotalScore(str); str=str+"总分:"+totalScore; System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); } bufferRead.close(); bufferWrite.close(); } catch(IOException e){ System.out.println(e.toString()); } } } 3. 结果截图: 4. 实验分析: 1. 改进程序,使得能统计出每个学生的平均成绩。 答: 2. 现在有如下格式的货物明细(文本格式)goods.txt 品名:电视,length:102 cm,width:89 cm,height:56 cm. 品名:轿车,length:4502 cm,width:178 cm,height:156 cm. 品名:桌子,length:125 cm,width:78 cm,height:68 cm. 答: CalculateVolume.java import java.io.*; import java.util.*; public class CalculateVolume{ public static void main(String args[]){ File fRead=new File("goods.txt"); File fWrite=new File("goodsVolume.txt"); try{Writer out=new FileWriter(fWrite,true); BufferedWriter bufferWrite=new BufferedWriter(out); Reader in=new FileReader(fRead); BufferedReader bufferRead=new BufferedReader(in); String str=null; while((str=bufferRead.readLine())!=null){ double s=Jisuan.getVolume(str); str=str+"体积:"+s+"cm^3"; System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); } bufferRead.close()

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧