Topic: 如何把屏幕上输入的信息写到文件?

  Print this page

1.如何把屏幕上输入的信息写到文件? Copy to clipboard
Posted by: radyjx
Posted on: 2005-09-06 14:57

这是我写的程序,查了很长时间,有什么问题?
import java.io.*;

public class GetScreen
{
  public static void main(String[] args) throws Exception
  {
    BufferedInputStream bi = new BufferedInputStream(System.in);
    FileOutputStream fileOut = new FileOutputStream("e:\\radyjx\\doc\\java\\study\\a.txt");
    BufferedOutputStream bo = new BufferedOutputStream(fileOut);
    
    int flag = 0;
    while(flag != 10)
    {
      flag = bi.read();
      System.out.print((char) flag);
      bo.write(flag);
    }
  }
}

2.Re:如何把屏幕上输入的信息写到文件? [Re: radyjx] Copy to clipboard
Posted by: dengyin2000
Posted on: 2005-09-07 09:42


package com.dengyin.com;

import java.io.*;

public class GetScreen
{
public static void main(String[] args) throws Exception
{
BufferedInputStream bi = new BufferedInputStream(System.in);
FileOutputStream fileOut = new FileOutputStream("c:\\a.txt");
BufferedOutputStream bo = new BufferedOutputStream(fileOut);
try{
char c = 0;
while((c = (char) bi.read())!='q'){
bo.write(c);
bo.flush();
}
}finally{
if (bi!=null) bi.close();
if (bo!=null) bo.close();
if (fileOut!=null) fileOut.close();
}

}
}

3.Re:如何把屏幕上输入的信息写到文件? [Re: radyjx] Copy to clipboard
Posted by: JavaandC
Posted on: 2005-09-07 14:16

你這個程序應該這樣寫就沒問題了,看註釋
mport java.io.*;

public class GetScreen
{
public static void main(String[] args) throws Exception
{
BufferedInputStream bi = new BufferedInputStream(System.in);
FileOutputStream fileOut = new FileOutputStream("e:/a.txt");
BufferedOutputStream bo = new BufferedOutputStream(fileOut);

int flag = 0;
while(flag != 10)
{
flag = bi.read();
System.out.print((char) flag);
bo.write(flag);
}
bo.close();//你的數據流沒有關閉,所以數據就沒有寫進去。
}
}

4.Re:如何把屏幕上输入的信息写到文件? [Re: radyjx] Copy to clipboard
Posted by: JavaandC
Posted on: 2005-09-07 14:17

忘了,還有bi.close();也應該關閉。

5.Re:如何把屏幕上输入的信息写到文件? [Re: radyjx] Copy to clipboard
Posted by: liner09136
Posted on: 2005-09-10 14:44

呵呵,我最近也在学。学习中。。SmileBeerBeerBeerBeerBeerBeerBeerBeer

6.Re:如何把屏幕上输入的信息写到文件? [Re: radyjx] Copy to clipboard
Posted by: CunningFox
Posted on: 2005-09-22 10:04

import java.io.*;

public class GetScreen{

  public static void main(String[] args) throws Exception{
    OutputStream os = null;
    InputStream is = null;
    
    try{
    os = new BufferedOutputStream(new FileOutputStream("a.txt"));
    is = new BufferedInputStream(System.in);
    int i;
    
    while((i=is.read())!=10)
    {      
      os.writeLight Bulb;        
    }
    }catch(Exception e){
      System.out.printlnEnvelope;
    }finally{
      is.close();
      os.close();
    }    
  }
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923