File file = new File("f:\\a.txt"); OutputStream oStream = new FileOutputStream(file); OutputStreamWriter oWriter = new OutputStreamWriter(oStream); BufferedWriter bWriter = new BufferedWriter(oWriter); bWriter.write("i am a good boy!!!"); bWriter.write("\n"); bWriter.write("yes!!!"); bWriter.flush(); bWriter.close(); //txt文件內容讀入到mysql數據庫 public static void txToMysql(File file) throws Exception { InputStream iStream = new FileInputStream(file); InputStreamReader iReader = new InputStreamReader(iStream); BufferedReader bReader = new BufferedReader(iReader); Connection conn = MysqlDBUtil.getConnection(); //insert into student values(2,'miss',STR_TO_DATE('2016-10-23','%Y-%m-%d')); PreparedStatement pstmt = conn.prepareStatement("insert into student values(?,?,STR_TO_DATE(?,'%Y-%m-%d'))"); String line; while((line = bReader.readLine()) != null){ String[] arr = line.split(","); pstmt.setInt(1, Integer.parseInt(arr[0])); pstmt.setString(2, arr[1]); pstmt.setString(3, arr[2]); pstmt.addBatch(); } pstmt.executeBatch(); }