- 输入输出流
- 字节流
- 字节流 + 缓冲流
- 字节输入输出流 + utf-8( 不考)
- 面向对象
- 长方形求面积
- Test类
- 长方形类
- 多线程
- 继承thread
- Test
- run
- Runnable接口
- Tesr
- run
package qimo; import java.io.*; public class Inout { public static void main(String[] args){ File i = new File("D:/java0/tzg","123.txt"); File j = new File("D:/java0/tzg","456.txt"); try { FileReader in = new FileReader(i); FileWriter out = new FileWriter(j); char[] st = new char[10]; int len=0; while((len = in.read(st))>0) { String str = new String(st, 0, len); out.write(st); System.out.print(str); // 输出每次 读入文件的内容 } out.close(); // 先关 out 再关 in in.close(); } catch (IOException e) { e.printStackTrace(); System.out.println(e.toString()); } } }字节流 + 缓冲流
package qimo; import java.io.*; public class Inout { public static void main(String[] args){ File i = new File("D:/java0/tzg","123.txt"); File j = new File("D:/java0/tzg","456.txt"); try { FileReader in = new FileReader(i); FileWriter out = new FileWriter(j); BufferedReader in1 = new BufferedReader(in); // 缓冲流 BufferedWriter out1 = new BufferedWriter(out); String st = null; while((st = in1.readLine()) != null) // 一次读一行 { String str = new String(st); out1.write(st); out1.newline(); System.out.print(str+'n'); // 输出每次 读入文件的内容 } // 先关out1 再关out out1.close(); // 先关out 再关 in out.close(); in1.close(); in.close(); } catch (IOException e) { e.printStackTrace(); System.out.println(e.toString()); } } }字节输入输出流 + utf-8( 不考)
package qimo; import java.io.*; public class inoutzijie { public static void main(String[] args){ File i = new File("D:/java0/tzg/","123.txt"); File j = new File("D:/java0/tzg/","456.txt"); try { FileInputStream in1 = new FileInputStream(i); FileOutputStream out1 = new FileOutputStream(j,true); // FileOutputStream(String name, boolen append) append 为true //在原文件后面追加 默认false 刷新文件 再写入 InputStreamReader in2 = new InputStreamReader(in1,"utf-8"); OutputStreamWriter out2 = new OutputStreamWriter(out1,"utf-8"); BufferedReader in3 = new BufferedReader(in2); BufferedWriter out3 = new BufferedWriter(out2); String st=null; while((st = in3.readLine()) != null) { String str = new String(st); out3.write(st); out3.newline(); System.out.println(str); } out3.close(); out2.close(); out1.close(); in3.close(); in2.close(); in1.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }面向对象 长方形求面积 Test类
package qimo; import java.util.Scanner; public class Tes { public static void main(String[] agrs){ Scanner re = new Scanner(System.in); System.out.print("请输入长:"); double a = re.nextDouble(); System.out.print("请输宽:"); double b = re.nextDouble(); Rectangle r = new Rectangle(a,b); System.out.println("长方形的面积是:"+r.area()); } }长方形类
package qimo; public class Rectangle { double a; double b; Rectangle(){} Rectangle(double a,double b) { this.a = a; this.b = b; } public void setA(double a) { this.a = a; } public void setB(double b) { this.b = b; } public double getA() { return a; } public double getB() { return b; } public double area() { return a * b; } }多线程 继承thread Test
package qimo; public class Test2 { public static void main(String[] args){ Carthead car = new Carthead(); car.start(); } }run
package qimo; public class Carthead extends Thread { int number=20; public void run(){ for(int i=1;i<=number;i++) { System.out.println("卖出了"+i+"火车票"); if(i == 0){} try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }Runnable接口 Tesr
package qimo; public class Test3 { public static void main(String[] args){ Carthead2 ca = new Carthead2(); Thread car = new Thread(ca); car.start(); } }run
package qimo; public class Carthead2 implements Runnable{ @Override public void run() { int n=20; for(int i=1;i<=n;i++) { System.out.println("火车票剩余"+(n-i)); if( i == 20 ){System.out.println("已售完");} try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)