BufferReader.PrintWriter

来源:互联网 发布:js控制浏览器最小化 编辑:程序博客网 时间:2021/11/29 08:02
package InputStreamReaderDemo;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;public class WorkingText {public static void main(String[] args) throws IOException{/* * 将得到的题目保存在title中 */List<Choice> titles=GetTitles("选择题");/* * 迭代将问题输出,等待输入答案,成功输出下一题 */for (Choice choice : titles) {System.out.print(choice);BufferedReader In=new BufferedReader(new InputStreamReader(System.in));System.out.println("请输入答案:");String str=In.readLine();if(str.charAt(0)==choice.getAnswer()){System.out.println("恭喜您答对了,进行下一题!");}else break;}}private static List<Choice> GetTitles(String string) throws IOException {//BufferedReader In=new BufferedReader(new InputStreamReader(new FileInputStream(string)));BufferedReader In=new BufferedReader(new FileReader(string));List<Choice> titles=new ArrayList<Choice>();String str;while((str=In.readLine())!=null){Choice choice=new Choice();String id;String text;List<String> answerList=new ArrayList<String>();char answer;id=str.substring(0,str.indexOf("."));text=str.substring(str.indexOf(".")+1);answerList.add(In.readLine());answerList.add(In.readLine());answerList.add(In.readLine());answerList.add(In.readLine());answer=In.readLine().charAt(0);choice.setId(Integer.parseInt(id));choice.setText(text);choice.setAnswerList(answerList);choice.setAnswer(answer);titles.add(choice);}In.close();return titles;}}

package InputStreamReaderDemo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;

public class InputStreamReaderDemo {

 public static void main(String[] argsr)throws IOException {
  InputStream is=new FileInputStream("li");
  InputStreamReader In=new InputStreamReader(is);
  int a=In.read();
  byte[] bt=new byte[4];
  bt[0]=(byte) (a&0xf);
  bt[1]=(byte) ((a>>4)&0xff);
  bt[2]=(byte) ((a>>8)&0xff);
  bt[3]=(byte) ((a>>12)&0xff);
  String str=new String(bt, "gbk");
  System.out.println(str);
  
  InputStreamReader In1=new InputStreamReader(new FileInputStream("li"));
  
  Reader red=new FileReader("li");
  is.close();
  In.close();
  In1.close();
  red.close();
  OutputStreamWriter wr=new OutputStreamWriter(new FileOutputStream("li"));
  Writer wrt=new FileWriter("li");
  wr.close();
  wrt.close();
  PrintWriter pw=new PrintWriter(new FileWriter("li"));//最常见的输出文件方式
  pw.print("加油");
  //必须存在以下两步骤,否则未写入。
  //pw.flush();
  pw.close();
 }

}


原创粉丝点击