Java编程

廖雪峰java6io编程

IO绠€浠?- Java IO缂栫▼ – 椋炴壃瀛﹂櫌.pdf 寤栭洩宄扮殑java鏁欑▼绫诲瀷锛?span class=”info”>鏂囨。(.pdf)澶у皬锛?span class=”info”>236KB鏃堕棿锛?span class=”info”>2019-01-20 18:31:19鏉ユ簮锛?span class=”info”>鐧惧害缃戠洏鐩綍锛?span class=”info”>/寤栭洩宄扮殑java鏁欑▼/6-Java IO缂栫▼/1-IO鍩虹鏌ョ湅鍒嗕韩鏂囦欢。

廖雪峰java6io编程

下列Java代码中的变量a、b、c分别在内存的__存储区存放?class A {private String a = “aa”;public boolean methodB() {String b = “bb”;final String c = “cc”;}}堆区、栈区、栈区。

package cn.starry.gangchat.utils;23 import java.io.ByteArrayOutputStream;4 import java.io.IOException;5 import java.io.InputStream;67 public class StreamUtils {8 public static String streamToString(InputStream in) {9 String result = “”;10 try {11 // 创建一个字节数组写入流12 ByteArrayOutputStream out = new ByteArrayOutputStream();13 byte[] buffer = new byte[1024];14 int length = 0;15 while ((length = in.read(buffer)) != -1) {16 out.write(buffer, 0, length);17 out.flush();18 }19 result = new String(out.toByteArray(), “utf-8”);20 out.close();21 } catch (IOException e) {22 e.printStackTrace();23 }24 return result;25 }26 }StreamUtils.java。

Java中常用的类,包,接口。class: ‘Date’,’System’,’Calender’,’Math’,’ArrayList’,’HashMap’package: ‘java.lang’,’java.util’,’java.io’,’java.sql’,’java.net’interface: ‘Collection’,’Map’,’List’,’Runnable’,’Callable。

package IO;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import sun.print.resources.serviceui;@SuppressWarnings(“serial”)class Book implements Serializable{private String title;private Double price;private transient String content;//瞬时 不会被序列化public void setTitle(String title) {this.title = title;}public String getTitle() {return title;}public void setPrice(Double price) {this.price = price;}public Double getPrice() {return price;}public void setContent(String content) {this.content = content;}public String getContent() {return content;}@Overridepublic String toString() {return “Book [title=” + title + “, price=” + price + “, content=” + content + “]”;}}public class SerializableDemo {public static void main(String[] args) throws IOException, ClassNotFoundException {ser();//dser();}/** 序列化*/public static void ser() throws FileNotFoundException, IOException{Book book = new Book();book.setPrice(12.8);book.setTitle(“admin”);ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(“E:” + File.separator + “person.ser”)));oos.writeObject(book);oos.close();}/** 反序列化*/public static void dser() throws FileNotFoundException, IOException, ClassNotFoundException{ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(“E:” + File.separator + “person.ser”)));Book per = (Book) ois.readObject();ois.close();System.out.println(per);}}transient 关键字修饰的属性不会被序列化6.IO的设计模式(装饰者模式 Decorator Pattern)。

Windows:C:\conf\default.propertiesLinux:/User/admin/conf/default.properties//先获取getClass(),再通过getResourceAsStream可以获取任意的资源文件try(InputStream input = getClass().getResourceAsStream(“/default.properties”)){if(input != null){//如果资源文件在classpath未找到,会返回null}}public class Main {public static void main(String[] args) throws IOException {String[] data1 = {“setting.properties”,//正确路径”/com/testList/setting.properties”,//正确路径”/com/setting.properties”,//错误路径”src/main/java/com/setting.properties”,//错误路径};for(String data:data1){getProperty(data);}String[] data2 = {“Person.txt”,//正确路径”/com/testList/Person.txt”,//正确路径”/com/List/Person.txt”,//错误路径”/src/main/java/com/testList/Person.txt”,//错误路径};for(String data:data2){getText(data);}}static void getProperty(String data) throws IOException{try(InputStream input = Main.class.getResourceAsStream(data)) {if(input != null){System.out.println(data+”文件已读取”);Properties props = new Properties();props.load(input);System.out.println(“url=”+props.getProperty(“url”));System.out.println(“name=”+props.getProperty(“name”));}else{System.out.println(data+”文件不存在”);}}}static void getText(String data) throws IOException{try(InputStream input = Main.class.getResourceAsStream(data)){if(input != null){System.out.println(data+”文件已读取”);BufferedReader reader = new BufferedReader(new InputStreamReader(input));//BufferedReader有readline()方法可以读取第一行文本System.out.println(reader.readLine());}else{System.out.println(data+”文件不存在”);}。

System.out.println(“数据:【” + new String(data, 0, foot) + “】”);。

System.out.println(“数据:【” + new String(data, 0, len) + “】”);。

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注