我想实现下用FileOutputtream往文本中写入信息,信息的来源是通过System.in.read的键盘输入,但是System.in.read返回的是整形,而BufferedReader的写入方法write(String)中要用String.怎样把整形转换成字符串,程序如下,程序中我用char数组遍历能实现,可是不想这样做,还请高手帮忙
import java.io.*;public class InOutTest
{
public void Systeminout(String path) throws IOException
{
FileOutputStream fos=new FileOutputStream(path);
OutputStreamWriter osr=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osr);
char[] s=new char[100];
int i=0;
for(i=0;i<10;i++)
{
int strline=System.in.read();
s[i]=(char)strline;
}
bw.write(s);
bw.close();
String sl;
FileInputStream fis=new FileInputStream(path);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
while((sl=br.readLine())!=null)
{
System.out.println(sl);
}
}
public static void main(String[] args) throws IOException
{
InOutTest iot=new InOutTest();
iot.Systeminout("E:\\java学习\\2.txt");
}
}
import java.io.*;public class InOutTest
{
public void Systeminout(String path) throws IOException
{
FileOutputStream fos=new FileOutputStream(path);
OutputStreamWriter osr=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osr);
char[] s=new char[100];
int i=0;
for(i=0;i<10;i++)
{
int strline=System.in.read();
s[i]=(char)strline;
}
bw.write(s);
bw.close();
String sl;
FileInputStream fis=new FileInputStream(path);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
while((sl=br.readLine())!=null)
{
System.out.println(sl);
}
}
public static void main(String[] args) throws IOException
{
InOutTest iot=new InOutTest();
iot.Systeminout("E:\\java学习\\2.txt");
}
}
解决方案 »
- apache HttpClient Content-Length 我用HttpClient调人家的服务,如何手动设置请求时Content-Length的值?
- PowerDesigner15.0的界面问题
- 我用s2sh框架同事只用struts2框架我把用户id什么的放在session里他能接到么如何接
- 关于Struts2的检验框架的问题
- 困惑 烦恼 辞还是不辞?
- 请教一个apache的问题。急!急!急!
- 问一下在web.xml里面这个是什么意思?
- 哪位大哥能不能给个webService客户端的调用程序?
- struts能和纯html写的jsp一起使用吗?怎样使用?
- eclipse + hibernate + tanghan plug_in
- Struts 2 下,用iText获得ServletOutputStream,怎样传回给客户端?
- 请大家给点建议
就这个问题???
int a = 333;
String b = a +"";
就转换了~
你是说想把这个整形(是char字符对应的整形值),转换为对应输入的字符吧?
好像只能这样写,因为要转换为对应的char值。
你是想直接一起转换吧?
这个我也不知道了,我记得好像没有。
不好意思哦,没给你解决问题~
呵呵~~~
你看看可以有其他方法直接得到String
而不是int的没~~
Scanner in=new Scanner(System.in);
String s= in.nextLine();
String s= in.nextLine();
额,偶也想起来这句话了
我刚才就是在找这个,1年多没见过这个语句了~
都忘记Scanner,,
还好这里有加深记忆了~
同样可以- -!
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
System.out.println(s);
通常都是这样将字节流包装起来成为字符流的,虽然scanner也很好用