写了一个读入文件,并按格式重新输出文件的程序。
运行时:能正常读入文件,并能成果创建输出文件;但是未能将读入的内容重新写入到新建的文件里。
请大侠,为小女指点迷津~!代码如下:
…………
………………数据读入:
BufferedReader in=new BufferedReader(new FileReader("data.txt");
String s=in.readLine();
StringTokenizer t=new StringTokenizer(s,"(,));
……
……        //省略代码的功能为保存读入数据创建新文档,并将读入的数据重新写入:
PrintWriter out=new PrintWriter(new FileOutputStream("refresh.txt"));
out.println(la+","+lo);         //la,lo为保存读入数据的变量;Double型程序运行无报错,就是新建了一个"refresh.txt"的空白文档,la、lo的数据没有被写入其中,不知道问题出在哪里?
请高人指点,小女感激不尽。

解决方案 »

  1.   

    好像有人问过一次了吧..  用 FileReader  跟 FileWriter..    
      

  2.   

    啥意思?不明白……
    难道创建文件,和像文件里写东西,要分别调用两个方法?
    println不能直接像文件里写内容吗?
      

  3.   


    import java.io.*;
    public class PrintWriterUse {
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    BufferedReader in = null;
    PrintWriter out = null;
    try {
    in = new BufferedReader(new FileReader("d:/data.txt"));//你想读的文件 
                                //你想将内容写到这个文件里面,如果没有,会帮你新建一个"refresh.txt"
    out=new PrintWriter(new FileOutputStream("d:/refresh.txt")); 
    String s=in.readLine(); 
    out.println(s);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    try {
    in.close();
    out.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }}上面的我刚写的, 可以输出哦!
    你那变量中存放了数据没,不知道!没有代码!但是我看你写出的代码中,是将“data.txt”以每行的读取方式存到了s里面了啊,你试着out.println(s);
    然后把其他的那些变量屏蔽掉,再看看可以打印出来不;
      

  4.   

    你用的BufferedReader读入就用BufferedWriter来写嘛!!!
      

  5.   

    可以用printwirter 输入,你new printWriter的时候别用outputstream用 filewriter或者file 应该好使了
      

  6.   


    try {
    FileOutputStream f = new FileOutputStream("E:/bb.txt", true);
    f.write((m1.group() + " ").getBytes());
    f.close();

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

  7.   

    一般用到缓冲时记得用close()或者flush(),不然有时候虽然正确却由于缓冲的缘故不一定会显示到
      

  8.   

    是这样的,流必须close或flush才能完成输出,还有是我写的,结构比较完善,不过是复制一个图片的.
    public class MyTestStream 
    {
    public static void main(String[] args) 
    {
    InputStream input = null;
    OutputStream output = null;
    try
    {
    input = new FileInputStream("a.jpg");
    output = new FileOutputStream("copy_a.jpg");

    int a;
    while((a = input.read()) != -1)
    {
    output.write(a);
    System.out.println(a + "  ");
    }
    }
    catch(Exception e){}

    finally
    {

    try 
    {
    if (input != null)
    input.close();
    } catch (IOException e1) 
    {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    try 
    {
    if (output != null)
    output.close();
    }
    catch (IOException e) 
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    System.out.println("over");
    }
    }
    }
      

  9.   

    11楼:
    while((a = input.read()) != -1) 
    这个判断语句是干啥的?不懂。