62,635
社区成员




import java.io.*;
/*程序中数据之间转换有问题,想输入数字,用int型,结果输出时为阿斯科编码值,并且a=(int)System.in.read();从键盘接受数据时,只能接收个位数,就是输入数据是中间不能有其他符号,空格也不行比如输入7894561230这样的格式才正确,然后会输出123456789*/
public class DD{
public static void main(String args[])throws IOException
{
int a=0;//希望有时间的高手可以修改这程序,当然没时间也可一直接给个解决问题的代码
int arraya[]=new int[1024];
int pos=0;
try{
File f=new File("sava.txt");
FileOutputStream fops1=new FileOutputStream(f);
System.out.println("输入一串数字,以0结束");
a=(int)System.in.read();//将从键盘上输入的字符赋给字符变量a,读入的不是数字,而是数字的阿斯科编码值
while(a!='0')
{
arraya[pos++]=a;
fops1.write(a);//将变量a中的字符写入磁盘文件中,打开文件后会看到数字,而不是阿斯科编码值(为何会这样?)
a=(int)System.in.read();
}
fops1.close();
pos=pos-1;//由于运用while,会增加对数字0的统计,所以减去1,但是由于数组中统计着0,需改进
FileOutputStream fops2=new FileOutputStream(f,true);//运用参数true,会将数据输入到文件尾部
System.out.println("总共有:"+(pos+1)+"个数字需要排序:");
int j,i,t;
for(i=0;i<=pos;i++)
for(j=1;j<=pos-i;j++)
{
if(arraya[j]<arraya[j-1])
{
t=arraya[j];
arraya[j]=arraya[j-1];
arraya[j-1]=t;
}
}
System.out.print("排序后的数字为:");
for(i=0;i<=pos;i++)
{
System.out.print("\t"+(char)arraya[i]);//利用强制转换类型才解决了输出阿斯科编码的问题,但没从源头上解决问题
fops2.write(arraya[i]);
}
fops2.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
package com.perficient.javabasic.test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class IOTesting {
public static void main(String[] args) throws IOException {
int c;
InputStreamReader in = new InputStreamReader(System.in);
OutputStreamWriter out = new OutputStreamWriter(System.out);
System.out.println("Please Enter the strings ended by Enter key:");
while ((c = in.read())!= '\n'){
out.write(c);
}
out.close();
in.close();
}
}
import java.io.*;
class PipedStreamTest
{
public static void main(String[] args)
{
PipedOutputStream pos=new PipedOutputStream();
PipedInputStream pis=new PipedInputStream();
try
{
pos.connect(pis);
new Producer(pos).start();
new Consumer(pis).start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Producer extends Thread
{
private PipedOutputStream pos;
public Producer(PipedOutputStream pos)
{
this.pos=pos;
}
public void run()
{
try
{
pos.write("Hello,welcome you!".getBytes());
pos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Consumer extends Thread
{
private PipedInputStream pis;
public Consumer(PipedInputStream pis)
{
this.pis=pis;
}
public void run()
{
try
{
byte[] buf=new byte[100];
int len=pis.read(buf);
System.out.println(new String(buf,0,len));
pis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}