标签:
import java.io.*; class Demo1 { public static void main(String[] args)throws IOException { /* PrintStream:打印流,具备字节输出流的基本功能,添加了打印功能 是一个可以向目的写入的输出流 目的:File 字符串形式的路径 字节输出流 */ PrintStream ps = new PrintStream("tt.txt"); //一次写入一个字节 //ps.write(353);//00000000 00000000 00000001 01100001 把低8位的一个字节写入 ps.println(353);//按照数据原样儿写入,也就是数据的表现形式,内部使用了转成字符串的功能,String.valueOf() ps.write(String.valueOf(353).getBytes()); } }
import java.io.*; class Demo2 { public static void main(String[] args) throws IOException { /* PrintWriter:字符打印流,字符输出流的基本功能都具备 目的:File 字符串形式的路径 字节输出流 字符数出流 */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //PrintWriter pw = new PrintWriter(System.out,true); PrintWriter pw2 = new PrintWriter(new FileWriter("t.txt"),true); String line = null; while((line = br.readLine())!=null) { if("over".equals(line)) break; pw2.println(line); //pw.flush(); } } }
//序列流:SequenceInputStream import java.io.*; import java.util.*; class Demo3 { public static void main(String[] args) throws IOException { /* FileInputStream fis1= new FileInputStream("Demo1.java"); FileInputStream fis2= new FileInputStream("Demo2.java"); FileInputStream fis3= new FileInputStream("Demo3.java"); Vector<FileInputStream> v= new Vector<FileInputStream>(); v.add(fis1); v.add(fis2); v.add(fis3); Enumeration<FileInputStream> en = v.elements();*/ ArrayList<FileInputStream> list = new ArrayList<FileInputStream>(); for(int i=1;i<=3;i++) { list.add(new FileInputStream("Demo"+i+".java")); } Enumeration<FileInputStream> en = Collections.enumeration(list); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("hebing2.java"); byte[] b = new byte[1024]; int len = 0; while((len = sis.read(b))!=-1) { fos.write(b,0,len); } sis.close(); fos.close(); } }
//分割文件 import java.io.*; import java.util.*; class Demo4 { public static void main(String[] args)throws IOException { File file = new File("C:\\Users\\qq\\Desktop\\ok.jpg"); splitFile(file); } //分割文件 public static void splitFile(File file)throws IOException { if(!file.isFile()) { System.out.println("不是文件"); return; } //创建存储被分割出来的文件的目录 File dir = new File("fenge"); if(!dir.exists()) dir.mkdir(); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = null; byte[] arr = new byte[1024*1024*2]; int len = 0; int num = 1; while((len = fis.read(arr))!=-1) { fos = new FileOutputStream(new File(dir,(num++)+".suipian")); fos.write(arr,0,len); } //这几个被分割出来的文件上传之后,使用者不知道文件类型和一共有 //几个分割出的文件,应该把这些信息以配置文件的形式告诉使用者 Properties pro = new Properties(); pro.setProperty("filetype",file.getName()); pro.setProperty("fileNumber",Integer.toString(--num)); fos = new FileOutputStream(new File(dir,"config.properties")); pro.store(fos," "); fis.close(); fos.close(); } }
import java.io.*; class Person implements Serializable//标记接口 { static final long serialVersionUID = 42L; private /*static*/ String name;//被静态修饰的成员不参与序列化 private /*transient*/ int age; //瞬态的成员不参与序列化 public Person(){} public Person(String name,int age) { this.name = name; this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } } class Demo5 { public static void main(String[] args)throws IOException,ClassNotFoundException { //writeObject(); readObject(); } //反序列化 //对象是依赖于其所属的类的,有那个class才会有对象 public static void readObject()throws IOException,ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt")); Person person = (Person)ois.readObject();//5964614836505496678 -2747677651716560467 System.out.println(person.getName()+","+person.getAge()); } //写入对象 //序列化:把一个对象写入到一个目的--对象的持久化 public static void writeObject()throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt")); oos.writeObject(new Person("lisi",23));//5964614836505496678 Person.class oos.close(); } }
import java.io.*; class Demo6 { public static void main(String[] args)throws IOException { /* RandomeAccessFile: 随机访问文件的流类 只能访问文件 内部既有字节输入流也有字节输出流 内部定义了一个字节数组,并通过指针操作该数组,所以能随机访问 */ //writeFile(); //readFile(); rw(); } public static void rw()throws IOException { RandomAccessFile raf = new RandomAccessFile("rw.txt","rw"); raf.seek(30); raf.writeUTF("你好"); long index = raf.getFilePointer(); System.out.println("index="+index); raf.seek(30); String ss = raf.readUTF(); System.out.println(ss); } public static void readFile()throws IOException { RandomAccessFile raf = new RandomAccessFile("temp.txt","r"); byte[] arr = new byte[4]; int len = 0; len = raf.read(arr); int age = raf.readInt(); System.out.println(new String(arr,0,len)+","+age); long index = raf.getFilePointer(); System.out.println("index="+index); raf.seek(15); len = raf.read(arr); age = raf.readInt(); System.out.println(new String(arr,0,len)+","+age); } public static void writeFile()throws IOException { //文件不存在会自动创建 RandomAccessFile raf = new RandomAccessFile("temp.txt","rw"); raf.write("刘能".getBytes()); raf.writeInt(58); long index = raf.getFilePointer();//得到指针指向的位置 raf.seek(15); raf.write("赵四".getBytes()); raf.writeInt(56); index = raf.getFilePointer(); System.out.println("index="+index); } }
import java.io.*; class Demo7 { public static void main(String[] args)throws IOException { /* 用于操作基本数据类型的流类 DataInputStream DataOutputStream */ writeData(); readData(); } public static void readData()throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("data.txt")); int a = dis.readInt(); boolean boo = dis.readBoolean(); double d = dis.readDouble(); System.out.println(a+","+boo+","+d); } public static void writeData()throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt")); dos.writeInt(34); dos.writeBoolean(false); dos.writeDouble(45.56); dos.close(); } }
import java.io.*; class Demo8 { public static void main(String[] args)throws IOException { /* 内存流: ByteArrayInputStream:内部有一个字节数组 ByteArrayOutputStream: 内部有一个字节数组,直接写入了自己内部的字节数组了 */ ByteArrayInputStream du = new ByteArrayInputStream("owieurwoeir".getBytes()); ByteArrayOutputStream xie = new ByteArrayOutputStream(); byte[] arr = new byte[1024]; int len = 0; while((len =du.read(arr))!=-1) { xie.write(arr,0,len); } System.out.println(new String(xie.toByteArray())); } }
import java.io.*; import java.util.*; import java.text.*; class Demo10 { public static void main(String[] args) throws IOException { try { int[] arr = new int[3]; System.out.println(arr[3]); } catch (Exception e) { Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss"); String ss = sdf.format(d); PrintStream ps = new PrintStream("bug.txt"); ps.println(ss); System.setOut(ps); e.printStackTrace(System.out); } } }
/* 二:有五个学生,每个学生有3门课的成绩, 从键盘输入以上数据(包括姓名,三门课成绩), 输入的格式:如:zhagnsan,30,40,60计算出总成绩, 并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。 1:描述一下学生 2:定义个操作学生的工具类 */ import java.io.*; import java.util.*; class Student implements Comparable<Student> { private String name; private int math,china,eng; private int sum; public Student(){} public Student(String name,int math,int china,int eng) { this.name =name; this.math = math; this.china = china; this.eng = eng; sum = math+china+eng; } public int compareTo(Student stu) { int num = this.sum-stu.sum; return num==0?this.name.compareTo(stu.name):num; } public int getSum() { return this.sum; } public int getMath() { return this.math; } public int getChina() { return this.china; } public int getEng() { return this.eng; } public String toString() { return "Student["+name+","+math+","+china+","+eng+"]"; } } //操作学生的工具类 class StuTool { public static Set<Student> getStudents() { return getStudents(null); } //读取键盘录入的学生信息,并存入集合 public static Set<Student> getStudents(Comparator<Student> com)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); TreeSet<Student> students; if(com!=null) students = new TreeSet<Student>(com); else students = new TreeSet<Student>(); String line = null; for(int i=1;i<=3;i++) { System.out.println("请输入第"+i+"个学员的信息"); line = br.readLine(); String[] arr = line.split(","); Student stu = new Student(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),Integer.parseInt(arr[3])); students.add(stu); } return students; } //把学员信息存入文件 public static void saveToFile(Set<Student> students)throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter("stud.txt")); for(Student stu:students) { bw.write(stu.toString()+"\t"); bw.write(""+stu.getSum()); bw.newLine(); bw.flush(); } bw.close(); } } class Demo11 { public static void main(String[] args) throws IOException { Comparator<Student> com = Collections.reverseOrder(); Set<Student> s = StuTool.getStudents(com); StuTool.saveToFile(s); } }
import java.io.*; class Demo12 { public static void main(String[] args)throws IOException { //转换流的编码问题 //writeFile(); readFile(); } public static void readFile()throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk.txt"),"utf-8"); char[] ch = new char[10]; int len = 0; len = isr.read(ch); System.out.println(new String(ch,0,len)); } public static void writeFile()throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"utf-8"); osw.write("你好"); osw.close(); } }
/* 编码:家里有事,速回。----》电报文 byte[] getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列 byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列 解码:电报文-----》家里有事,速回。 String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。 String(byte[] bytes, Charset charset) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String 编码编对了,解码就能解对 编码编错了,就不可能解对 */ import java.util.*; class Demo13 { public static void main(String[] args)throws Exception { String ss ="你好"; byte[] b = ss.getBytes("gbk"); System.out.println(Arrays.toString(b)); //解码解错了 String str = new String(b,"ISO8859-1"); System.out.println(str); //解决解码解错了的问题 byte[] arr = str.getBytes("ISO8859-1"); //使用gbk进行解码 String s = new String(arr); System.out.println(s); } }
class Demo14 { public static void main(String[] args) { String ss = "我的联通"; byte[] b = ss.getBytes(); for(byte num:b) { System.out.println(Integer.toBinaryString(num&255)); } } }
import java.io.*; class Demo1 { public static void main(String[] args)throws IOException { /* PrintStream:打印流,具备字节输出流的基本功能,添加了打印功能 是一个可以向目的写入的输出流 目的:File 字符串形式的路径 字节输出流 */ PrintStream ps = new PrintStream("tt.txt"); //一次写入一个字节 //ps.write(353);//00000000 00000000 00000001 01100001 把低8位的一个字节写入 ps.println(353);//按照数据原样儿写入,也就是数据的表现形式,内部使用了转成字符串的功能,String.valueOf() ps.write(String.valueOf(353).getBytes()); } } import java.io.*; class Demo2 { public static void main(String[] args) throws IOException { /* PrintWriter:字符打印流,字符输出流的基本功能都具备 目的:File 字符串形式的路径 字节输出流 字符数出流 */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //PrintWriter pw = new PrintWriter(System.out,true); PrintWriter pw2 = new PrintWriter(new FileWriter("t.txt"),true); String line = null; while((line = br.readLine())!=null) { if("over".equals(line)) break; pw2.println(line); //pw.flush(); } } } //序列流:SequenceInputStream import java.io.*; import java.util.*; class Demo3 { public static void main(String[] args) throws IOException { FileInputStream fis1= new FileInputStream("Demo1.java"); FileInputStream fis2= new FileInputStream("Demo2.java"); FileInputStream fis3= new FileInputStream("Demo3.java"); Vector<FileInputStream> v= new Vector<FileInputStream>(); v.add(fis1); v.add(fis2); v.add(fis3); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("hebing.java"); byte[] b = new byte[1024]; int len = 0; while((len = sis.read(b))!=-1) { fos.write(b,0,len); } sis.close(); fos.close(); } }
import java.io.*; class Demo1 { public static void main(String[] args)throws IOException { /* PrintStream:打印流,具备字节输出流的基本功能,添加了打印功能 是一个可以向目的写入的输出流 目的:File 字符串形式的路径 字节输出流 */ PrintStream ps = new PrintStream("tt.txt"); //一次写入一个字节 //ps.write(353);//00000000 00000000 00000001 01100001 把低8位的一个字节写入 ps.println(353);//按照数据原样儿写入,也就是数据的表现形式,内部使用了转成字符串的功能,String.valueOf() ps.write(String.valueOf(353).getBytes()); } } import java.io.*; class Demo2 { public static void main(String[] args) throws IOException { /* PrintWriter:字符打印流,字符输出流的基本功能都具备 目的:File 字符串形式的路径 字节输出流 字符数出流 */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //PrintWriter pw = new PrintWriter(System.out,true); PrintWriter pw2 = new PrintWriter(new FileWriter("t.txt"),true); String line = null; while((line = br.readLine())!=null) { if("over".equals(line)) break; pw2.println(line); //pw.flush(); } } } //序列流:SequenceInputStream import java.io.*; import java.util.*; class Demo3 { public static void main(String[] args) throws IOException { /* FileInputStream fis1= new FileInputStream("Demo1.java"); FileInputStream fis2= new FileInputStream("Demo2.java"); FileInputStream fis3= new FileInputStream("Demo3.java"); Vector<FileInputStream> v= new Vector<FileInputStream>(); v.add(fis1); v.add(fis2); v.add(fis3); Enumeration<FileInputStream> en = v.elements();*/ ArrayList<FileInputStream> list = new ArrayList<FileInputStream>(); for(int i=1;i<=3;i++) { list.add(new FileInputStream("Demo"+i+".java")); } Enumeration<FileInputStream> en = Collections.enumeration(list); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("hebing2.java"); byte[] b = new byte[1024]; int len = 0; while((len = sis.read(b))!=-1) { fos.write(b,0,len); } sis.close(); fos.close(); } }
反射
package com.qianfeng.reflect; public class Demo1 { public Demo1() { // TODO Auto-generated constructor stub } /** *反射:动态获取类(class)及类中成员,并对类中的成员运行 * *怎么获取到Class对象 * *动态获取Person.class对象 *获取方式有三种: *1:使用Object中的getClass()方法 * 需要先创建对象,所以的需要其所属的类实现存在 *:2:每个数据类型都有一个静态的属性class,通过该属性可以得到其所属的字节码文件对象 * 不需要创建对象了,但是需要那个类存在 * 3:Class.forName()只需要提供一个字符串,这个字符串的内容是类的名字 * */ public static void main(String[] args) throws ClassNotFoundException { //getClass1(); //getClass2(); getClass3(); } private static void getClass3() throws ClassNotFoundException { Class claz = Class.forName("com.qianfeng.reflect.Person"); System.out.println(claz); } private static void getClass2() { Class claz1 = Person.class; Class claz2 = Person.class; System.out.println(claz1==claz2); } private static void getClass1() { Person p1 = new Person(); Class claz = p1.getClass();//Person.class Person p2 = new Person(); Class claz2 = p2.getClass();//Person.class System.out.println(claz==claz2); } }
package com.qianfeng.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Demo2 { public Demo2() { // TODO Auto-generated constructor stub } /** * 动态获取类并创建对象 * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException */ public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { createObj1(); createObj2(); } //使用带参数的构造方法创建对象 private static void createObj2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { //Person p = new Person("lisi",23); Class claz = Class.forName("com.qianfeng.reflect.Person"); Constructor cons = claz.getConstructor(String.class,int.class); Object obj = cons.newInstance("lili",23); System.out.println(obj); } private static void createObj1() throws ClassNotFoundException, InstantiationException, IllegalAccessException { //Person p = new Person(); Class claz = Class.forName("com.qianfeng.reflect.Person");//Person.class Object obj = claz.newInstance();//用的是默认的空参数的构造方法 System.out.println(obj); } }
package com.qianfeng.reflect; import java.lang.reflect.Field; public class Demo3 { public Demo3() { // TODO Auto-generated constructor stub } /** * 动态创建对象并给属性赋值 * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws InstantiationException */ public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException { accessField1(); } //动态创建对象并给属性赋值 private static void accessField1() throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException { //Person p = new Person(); //p.name = ""; Class claz = Class.forName("com.qianfeng.reflect.Person"); //Field field = claz.getField("name");//只能获取公共的成员属性 Field field = claz.getDeclaredField("name");//只要是在类中的声明的就可以得到 //创建一个Person类型的对象 Object obj = claz.newInstance(); //设置可以访问该属性---暴力访问 field.setAccessible(true); field.set(obj, "zhangsan"); System.out.println(obj); } }
package com.qianfeng.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Demo4 { public Demo4() { // TODO Auto-generated constructor stub } /** * 动态执行类中的方法 * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { invokeMethod1(); invokeMethod2(); invokeMethod3(); } private static void invokeMethod3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class claz = Class.forName("com.qianfeng.reflect.Person"); Method m = claz.getMethod("fun",null); m.invoke(null,null); } private static void invokeMethod2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class claz = Class.forName("com.qianfeng.reflect.Person"); Method m = claz.getMethod("method", String.class); //创建对象 Object obj = claz.newInstance(); m.invoke(obj, "haha"); } //执行无参数的方法 private static void invokeMethod1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { //Person p = new Person(); //p.show(); Class claz = Class.forName("com.qianfeng.reflect.Person"); //得到方法 Method m = claz.getMethod("show",null); //创建对象 Object obj = claz.newInstance(); m.invoke(obj, null); } }
标签:
原文地址:http://blog.csdn.net/lc635408136/article/details/42102657