一:字節流讀寫app
//字節流讀寫
InputStream is = new FileInputStream("D:\\warn.log");
int read;
while((read=is.read())!=-1) {
System.out.print((char)read);
}
is.close();
OutputStream os = new FileOutputStream("D:\\warn.log",true);
String res = "hello word";
os.write(res.getBytes());
os.flush();
os.close();對象
二:字符流讀寫get
//字符流讀寫
Reader reader = new FileReader("D:\\\\warn.log");
BufferedReader br = new BufferedReader(reader);
String line = null;
while((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
reader.close();
Writer writer = new FileWriter("D:\\\\warn.log");
BufferedWriter bw = new BufferedWriter(writer);
bw.write("hello wang!!");
bw.flush();
writer.flush();
bw.close();
writer.close();it
三:字節流與字符流轉換class
//字節流與字符流的轉換
InputStream is = new FileInputStream("D:\\warn.log");
InputStreamReader bis = new InputStreamReader(is);
BufferedReader br = new BufferedReader(bis);
String line = null;
while((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
bis.close();
is.close();
OutputStream os = new FileOutputStream("D:\\warn.log",true);
OutputStreamWriter ow = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(ow);
bw.write("你好世界!");
bw.flush();
ow.flush();
os.flush();
bw.close();
ow.close();
os.close();序列化
四:JAVA序列化反射
//JAVA序列化與反序列化
OutputStream os = new FileOutputStream("D:\\warn.log");
ObjectOutputStream oos = new ObjectOutputStream(os);
Apple apple = new Apple(2.5);
oos.writeObject(apple);
oos.flush();
os.flush();
oos.close();
os.close();
InputStream is = new FileInputStream("D:\\warn.log");
ObjectInputStream ois = new ObjectInputStream(is);
Apple a = (Apple)ois.readObject();
System.out.println(a.getWeight());
ois.close();
is.close();方法
五:JAVA反射demo
//獲得class對象
Apple apple = new Apple();
Class cls1 = apple.getClass();
Class cls2 = com.infosys.demo.vo.Apple.class;
Class cls3 = Class.forName("com.infosys.demo.vo.Apple");
//獲得全部的屬性
Field[] fields = cls3.getDeclaredFields();
for(Field f:fields) {
System.out.println(f.getName());
System.out.println(f.getType());
}
//獲得當前類的普通方法
Method[] methods1 = cls3.getDeclaredMethods();
for(Method m:methods1) {
System.out.println(m.getName());
System.out.println(m.getGenericReturnType());
System.out.println(Arrays.toString(m.getGenericParameterTypes()));
}
//獲得構造方法
Constructor[] methods2 = cls3.getDeclaredConstructors();
System.out.println(Arrays.toString(methods2));word