从键盘输入一个字符串,将小写字母所有转换成大写字母,而后输出到一个磁盘文件"test"中保存java
思路比较简单,先用Scanner对象得到一个字符串。而后建立文件,而后在将字符串输入到指定的文件中网络
按照数据来源(去向)分类:
1.是文件:FileInputStream,FileOutputStream,FileReader,FileWriter
2.是byte[]:ByteArrayInputStream,ByteArrayOutputStream
3.是Char[]:CharArrayReader,CharArrayWriter
4.是String:StringBufferInputStream,StringReder,StringWriter
5.网络数据流:InputStream,OutputStram,Reader,Writerthis
按是否格式化输出
1.要格式化输出:PrintStream PrintWriter编码
按是否要缓冲分:
要缓冲:BufferedInputStream,BufferedOutputStream,BufferOutputStream,BufferWritercode
按照数据格式分:
1.二进制格式(只要不能七肯定是纯文本的):InputStream,OutputStream以其全部带Stream结束的子类
2.含应为和汉字或者其余编码方式:Reader,Writer及其全部带Reader,Writer的子类对象
按输入输出分:
1.输出:Reader,InputStream类型的子类
2.输出:Writer,OutputStream类型的子类
特殊须要:
1.从Stream到Reader,Writer的转换器,InputStreamReader,OutputStreamWriter
2.对象的出入输出:ObjectInputStream,ObjectOutputStream
3.进程间通讯:PipeInputStream,PipeOutputStream,PipeWriter,PipeWriter
4.合并输入:SequenceInputStream
决定使用哪一个类以及构造进程的准组:
1.考虑最原始的数据格式是什么
2.是输入仍是输出
3.是否须要转换流
4.数据的去向
5.是否须要缓冲
6.是否须要格式化输出。进程
Java.io.Reader和java.io.InputStream组成了Java输入类。Reader用于读入16位字符,也就是Unicode编码字符,而InputStream用于读入ASCII字符和二进制数据。
对应的输出类也有差很少的区别。ip
import java.io.*; import java.util.Scanner; public class Test{ private String inputStr; public void setInputStr(String Str){ this.inputStr=Str.toUpperCase(); } public String getInputStr(){ return this.inputStr; } public void Save(String path){ File file=new File(path); if(file.exists()){ System.out.println("建立单个文件"+path+"失败,目标文件已经存在"); } if(path.endsWith((File.separator))){ System.out.println("建立单个文件"+path+"失败,目标文件不能是目录"); } if(!file.getParentFile().exists()){ System.out.println("目标文件所在的目录不存在,准备建立它!"); if(!file.getParentFile().mkdirs()){ System.out.println("建立目标文件所在的目录失败!"); } } try{ Printstream(file); Filewriter(file); Printwriter(file); FileOutstream(file); }catch(Exception e){ e.printStackTrace(); } } public void Printstream(File file){ try{ PrintStream ps =new PrintStream(new FileOutputStream(file)); ps.println(this.getInputStr()); }catch(FileNotFoundException e){ e.printStackTrace(); } } public void Filewriter(File file){ try{ FileWriter fw=new FileWriter(file.getAbsolutePath(),true); BufferedWriter bw=new BufferedWriter(fw); bw.write(this.getInputStr()); bw.close(); fw.close(); }catch(IOException e){ e.printStackTrace(); } } public void Printwriter(File file){ try{ PrintWriter pw=new PrintWriter(new FileWriter(file.getAbsolutePath())); pw.println(this.getInputStr()); pw.close(); }catch(IOException e){ e.printStackTrace(); } } public void FileOutstream(File file){ try{ FileOutputStream fos=new FileOutputStream(file.getAbsolutePath()); fos.write(this.getInputStr().getBytes()); fos.close(); }catch(IOException e){ e.printStackTrace(); } } public static void main(String[] args){ Scanner in =new Scanner(System.in); String Str=in.nextLine(); Test temp=new Test(); temp.setInputStr(Str); temp.Save("D:\\temp\\test.txt"); } }