本人在学习java输入输出流中遇到的问题,现统计如下四个问题,求解决:
重点:收集io输入输出流的教程,有关io输入输出流的教程全要,然后就是求说明文件流读数据时的类型(比如定义的明明是int,结果输出变成阿斯科编码)问题,有使用io输入输出的程序,也请贴出代码,求学习。
1,求一篇大家看过之后觉得好的io输入输出流教程,不要视频的,不便于理解,主要针对Io中的数据类型问题,比如输入的是数字但是他显示时是数字,但是输出时是阿斯科编码值,具体请看下面的代码2:
2,题目:从键盘上接受数据,输出到文件中,然后对数据排序,输出到文件中,输出到显示器,利用io输入输出流
本人遇见最大的问题就是数据比较时,本来应该是int的,结果是阿斯科编码值,然后修改,出现众多问题,(总结,还是没学明白)详情见代码: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);
} }
}
重点:收集io输入输出流的教程,有关io输入输出流的教程全要,然后就是求说明文件流读数据时的类型(比如定义的明明是int,结果输出变成阿斯科编码)问题,有使用io输入输出的程序,也请贴出代码,求学习。
1,求一篇大家看过之后觉得好的io输入输出流教程,不要视频的,不便于理解,主要针对Io中的数据类型问题,比如输入的是数字但是他显示时是数字,但是输出时是阿斯科编码值,具体请看下面的代码2:
2,题目:从键盘上接受数据,输出到文件中,然后对数据排序,输出到文件中,输出到显示器,利用io输入输出流
本人遇见最大的问题就是数据比较时,本来应该是int的,结果是阿斯科编码值,然后修改,出现众多问题,(总结,还是没学明白)详情见代码: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);
} }
}
System.out:为PrintStream类型,标准输出流,默认为控制台输出
System.err:为PrintStream类型,代表错误输出流,默认为控制台输出 以下代码实现将键盘输入,输出到控制台,以回车键结束:
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();
}
}
过滤流:一些流可以从文件以及其他地方接收字节,另一些流可以将字节组合成更有用的数据类型。将一个已经存在的流传递给另一个流的构造方法,将这两种流结合起来,结合后的流被称为过滤流。
过滤器输入流往往是以其它输入流作为它的输入源,经过过滤或处理后再以新的输入流的形式提供给用户,过滤器输出流也类似。
java的常用输入、输出流
其实都是继承自4个抽象类,分别是
基于单字节的InputStream,OutputStream类
基于双字节的Unicode代码单元的 Reader, Writer类
一旦打开输入流后,程序就可从输入流串行地读数据。
从输入流读数据的过程一般如下:
open a stream
while more information
read information
close the stream 类似地,程序也能通过打开一个输出流并顺序地写入数据来将信息送至目的端。
往输出流写数据的过程一般如下:
open a stream
while more information
write information
close the stream
java.io包中的stream类根据它们操作对象的类型是字符还是字节可分为两大类: 字符流和字节流。
InputStream,OutputStream类仅仅读取和写入单个的字节和字节数组,它们没有读取和写入字符串和数值的方法。
由于以字节为单位的流处理存储为Unicode码的信息很不方便(Unicode的每个代码单元使用了两个字节),所以有了一个专门的类层次来处理Unicode字符,这些类继承于抽象类Reader和Writer。
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();
}
}
}