學了這麼多全是給他用的之Commonshtml
用這個的前提須要導包,java
①建立lib文件夾mysql
②將下載的commos-io.jar拷貝到lib文件夾sql
③右鍵點擊commons-io.jar,Build Path→Add to Build Pathapi
而後介紹兩個工具類oracle
這個工具類是用來處理文件名,他能夠輕鬆解決不一樣操做系統文件名稱規範不一樣的問題工具
l 經常使用方法:ui
getExtension(String path):獲取文件的擴展名;this
getName(String filename):獲取文件名;url
isExtension(String fileName,String ext):判斷fileName是不是ext後綴名;
//明確數據源 String str="D:\\demo0611\\e.txt"; //獲取後綴名 String path=FilenameUtils.getExtension(str); System.out.println("文件拓展名"+path); String name=FilenameUtils.getName(str); System.out.println("名字叫"+name); boolean flag=FilenameUtils.isExtension("D:\\demo0611\\e.txt","txt"); System.out.println(flag);
1.1 FileUtils
提供文件操做(移動文件,讀取文件,檢查文件是否存在等等)的方法。
l 經常使用方法:
readFileToString(File file):讀取文件內容,並返回一個String;
writeStringToFile(File file,String content):將內容content寫入到file中;
copyDirectoryToDirectory(File srcDir,File destDir);文件夾複製
copyFile(File srcFile,File destFile);文件複製
//文件的讀取 File file=new File("D:\\demo0611\\e.txt"); //讀取文件 String content=FileUtils.readFileToString(file); System.out.println(content); //複製文件夾 //明確數據源 File nowFile=new File("D:\\demo0611\\e.txt"); //明確目的地 File afterFile=new File("D:\\demo0611\\a\\vvv.txt"); //開始複製 FileUtils.copyFile(nowFile, afterFile);
Properties String 鍵值對,屬於Hashtable的子類,泛型就是String字符串
存儲:setProperty()
獲取:getProperty()
比較重要的是load,讀取屬性
它的主要操做是能夠寫一個文檔來存儲成對出現的值。
//用這個集合書寫的鍵值對 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/java0611?characterEncoding=utf8 username=root password=123456
public static Connection getConn(){ Properties pro=new Properties(); try { //明確數據源 FileReader fr=new FileReader("src/com/oracle/tools/db.properties"); pro.load(fr); } catch (IOException e1) { e1.printStackTrace(); } //直接獲取文件便可,之後方便 //得到連接對象 String url=pro.getProperty("url"); String username=pro.getProperty("username"); String password=pro.getProperty("password"); //爲了返回值,必須把它做爲全局變量 Connection conn=null; try { //註冊驅動 Class.forName(pro.getProperty("driver")); //得到鏈接對象 conn=DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }
public static void main(String[] args) throws IOException {
//序列化流,把對象寫到文件裏
//明確目的地,必須是字節
FileOutputStream fos=new FileOutputStream("D:\\demo0611\\person.txt");
//建立序列化流
ObjectOutputStream oos=new ObjectOutputStream(fos);
//向文件中寫入對象 有參構造方法,不須要new對象了
oos.writeObject(new Person("張三",18));
//釋放資源
oos.close();
}
他存在的意義在於能夠將按照自定義類書寫的對象經過對象序列化流存儲到文檔中。
由於是字節存儲因此亂碼。
//反序列化流
//明確數據源
FileInputStream fis=new FileInputStream("D:\\demo0611\\person.txt");
//建立反序列化
ObjectInputStream ois=new ObjectInputStream(fis);
//從文件中讀取對象 由於讀取的是萬物父類object因此須要向下轉型,讀取子類獨有元素。
Object obj=ois.readObject();
//向下轉型
if(obj instanceof Person){
Person p=(Person)obj;
//向下轉型以後就獲取到了Person對象
System.out.println(p);
}
//釋放資源
ois.close();
反序列化流是將自定義類的對象打印到程序裏。向下轉型是關鍵。調取子類獨有對象
}
person類
public class Person implements Serializable{
private transient String name; //不想被序列化修飾能夠用transient,這個是將內容進行序列化存儲,而靜態是直接在類加載時就完成存儲。
public static int age;
public static final long serialVersionUID =1000L; 序列化流的關鍵在於,若是不表示一個序列號那麼類一旦變化就會報錯,這樣就至關於生成了一個標識,獲取的就是它。
//有參構造
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
打印流:
字節打印流 PrintStream
字符打印流 PrintWriter
方法:
void print(String str): 輸出任意類型的數據,
void println(String str): 輸出任意類型的數據,自動寫入換行操做
打出來並本身更新。