zachaxy 2014-12-14 05:28 采纳率: 0%
浏览 2843

关于java的IO中PrintWriter问题

public class PrintIOTest {
public static void main(String[] ages){
try{
FileOutputStream fos = new FileOutputStream("test.txt");
PrintStream ps = new PrintStream(fos);
ps.print("hello,world!");

    }catch (FileNotFoundException e){
        e.printStackTrace();
    }


    /*try{
        FileWriter fw = new FileWriter("test.txt");
        PrintWriter pw = new PrintWriter(fw);
        pw.write("你好");
    }catch(IOException e){
        e.printStackTrace();
    }*/

}

}

用字节流外面套一个PrintStream运行正常,但是换成注释里的内容却只能新建test.tex,而不写内容,请问是怎么回事??

  • 写回答

3条回答

  • 全栈极简 博客专家认证 2014-12-14 05:32
    关注

    用这个:
    try {
    FileWriter fw = new FileWriter("test.txt");
    PrintWriter pw = new PrintWriter(fw);
    pw.write("你好");
    pw.close();
    } catch (IOException e) {
    e.printStackTrace();
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。