zoukankan      html  css  js  c++  java
  • Java 将字符串输入文件中

    题目介绍

    从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存

    思路分析

    思路比较简单,先用Scanner对象获得一个字符串。然后创建文件,然后在将字符串输入到指定的文件中

    使用类的对象

    Java IO使用原则:

    按照数据来源(去向)分类:
    1.是文件:FileInputStream,FileOutputStream,FileReader,FileWriter
    2.是byte[]:ByteArrayInputStream,ByteArrayOutputStream
    3.是Char[]:CharArrayReader,CharArrayWriter
    4.是String:StringBufferInputStream,StringReder,StringWriter
    5.网络数据流:InputStream,OutputStram,Reader,Writer

    按是否格式化输出
    1.要格式化输出:PrintStream PrintWriter

    按是否要缓冲分:
    要缓冲:BufferedInputStream,BufferedOutputStream,BufferOutputStream,BufferWriter

    按照数据格式分:
    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字符和二进制数据。
    对应的输出类也有差不多的区别。

    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");
    
        }
    }
    
  • 相关阅读:
    poj1580
    poj1607
    poj1313
    poj1314
    c语言之extern和static
    C笔记(一)
    搭建Linux高可用性集群(第一天)
    利用回调函数实现泛型算法
    关于SQL server中的 identity
    SQL(一)
  • 原文地址:https://www.cnblogs.com/NaCl/p/10495683.html
Copyright © 2011-2022 走看看