IO読み書きとJdbc添削
11311 ワード
文書ディレクトリバイトストリームInputStream、OutputStream バッファバイトストリームBufferedInputStream、BufferedOutputStream バッファフローBufferedReader、BufferedWriter PrintWriter (Reader、PrintWriter) NIO JDBC
バイトストリームInputStream、OutputStream
バッファバイトストリームBufferedInputStream、BufferedOutputStream
バッファフローBufferedReader、BufferedWriter
PrintWriter (Reader、PrintWriter)
NIO
JDBC
バイトストリームInputStream、OutputStream
// ( )
InputStream in = new FileInputStream("d:\\1.txt");
//
OutputStream out = new FileOutputStream("d:\\2.txt");
//
//
byte[] bytes = new byte[2048];
// (n , )
int n = -1;
//
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//
String str = new String(bytes,0,n,"GBK"); # ,
System.out.println(str);
//
out.write(bytes, 0, n);
}
//
in.close();
out.close();
バッファバイトストリームBufferedInputStream、BufferedOutputStream
// ( )
BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\1.txt"));
//
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:\\2.txt"));
//
//
byte[] bytes = new byte[2048];
// (n , )
int n = -1;
//
while ((n = in.read(bytes,0,bytes.length)) != -1) {
//
String str = new String(bytes,0,n,"GBK");
System.out.println(str);
//
out.write(bytes, 0, n);
}
//
out.flush();
//
in.close();
out.close();
バッファフローBufferedReader、BufferedWriter
// ( )
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK"));#
//BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt")));
//
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2.txt"),"GBK"));
//BufferedWriter out = new BufferedWriter(new FileWriter("d:\\2.txt"));
//
//
String str = null;
while ((str = in.readLine()) != null) {
System.out.println(str);
//
out.write(str);
out.newLine();
}
//
out.flush();
//
in.close();
out.close();
PrintWriter (Reader、PrintWriter)
// ( )
Reader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");
//
PrintWriter out = new PrintWriter(new FileWriter("d:\\2.txt"));
//
//
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read()) != -1) {
System.out.println(len);
//
out.write(len);
}
//
out.flush();
//
in.close();
out.close();
NIO
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
public class NIOtest {
static void readNIO() {
String pathname = "e:\\2222.txt";
FileInputStream fin = null;
try {
fin = new FileInputStream(new File(pathname));
FileChannel channel = fin.getChannel();
int capacity = 100;//
ByteBuffer bf = ByteBuffer.allocate(capacity);
System.out.println(" :" + bf.limit() + " :" + bf.capacity()
+ " :" + bf.position());
int length = -1;
while ((length = channel.read(bf)) != -1) {
/*
* , , 0, limit , , 0
*/
bf.clear();
byte[] bytes = bf.array();
//System.out.write(bytes, 0, length);
System.out.println(new String(bytes,"unicode"));
/*System.out.println(" :" + bf.limit() + " :" + bf.capacity()
+ " :" + bf.position());*/
}
channel.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static void writeNIO() {
String filename = "e:\\out.txt";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(filename));
FileChannel channel = fos.getChannel();
ByteBuffer src = Charset.forName("utf8").encode(" ");
// limit ,
System.out.println(" limit:" + src.capacity() + ","
+ src.limit());
int length = 0;
while ((length = channel.write(src)) != 0) {
/*
* , clear,
*/
System.out.println(" :" + length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static void testReadAndWriteNIO() {
String pathname = "e:\\1111.txt";
FileInputStream fin = null;
String filename = "e:\\test-out.txt";
FileOutputStream fos = null;
try {
fin = new FileInputStream(new File(pathname));
FileChannel channel = fin.getChannel();
int capacity = 100;//
ByteBuffer bf = ByteBuffer.allocate(capacity);
System.out.println(" :" + bf.limit() + " :" + bf.capacity()+ " :" + bf.position());
int length = -1;
fos = new FileOutputStream(new File(filename));
FileChannel outchannel = fos.getChannel();
while ((length = channel.read(bf)) != -1) {
// limit, 0, 0 limit ,
bf.flip();
int outlength =0;
while((outlength=outchannel.write(bf)) != 0){
System.out.println(" ,"+length+" ,"+outlength);
}
// 0, limit , 0 limit( ) ,
// ,
//0 limit
bf.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@SuppressWarnings("resource")
public static void main(String[] args) {
readNIO();
// writeNIO();
// testReadAndWriteNIO();
}
}
JDBC
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbctest {
// useUnicode=true&characterEncoding=UTF-8
String jdbcUrl = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String className = "com.mysql.jdbc.Driver";
String user = "root";
String password = "920619";
public Connection getConnection() {
Connection connection = null;
try {
//
Class.forName(className);
//
connection = DriverManager.getConnection(jdbcUrl, user, password);
} catch (Exception e) {
System.out.println(" ");
}
return connection;
}
public void closeConnection(Connection connection) {
try {
connection.close();
System.out.println(" ");
} catch (SQLException e) {
System.out.println(" ");
}
}
public void insert() {
String sql = "insert into user (UserName,PassWord,UserAge,UserSex) values(' ','123456', 20, 0)";
Connection connection = getConnection();
try {
//
Statement statement = connection.createStatement();
// SQL
int result = statement.executeUpdate(sql);
if (result != 0) {
System.out.println(" , " + result + " ");
}
statement.close();
} catch (SQLException e) {
System.out.println(" ");
} finally {
closeConnection(connection);
}
}
public void delete() {
String sql = "delete from user where UserId in (2,3,4)";
Connection connection = getConnection();
try {
Statement statement = connection.createStatement();
int result = statement.executeUpdate(sql);
if (result != 0) {
System.out.println(" , " + result + " ");
}
statement.close();
} catch (SQLException e) {
System.out.println(" ");
} finally {
closeConnection(connection);
}
}
public void update() {
String sql = "update user set UserName = ' ' where UserId = 5";
Connection connection = getConnection();
try {
Statement statement = connection.createStatement();
int result = statement.executeUpdate(sql);
if (result != 0) {
System.out.println(" , " + result + " ");
}
statement.close();
} catch (SQLException e) {
System.out.println(" ");
} finally {
closeConnection(connection);
}
}
public void select() {
// SQL , ?
String sql = "select * from user where UserId = ?";
Connection connection = getConnection();
try {
// SQL
PreparedStatement statement = connection.prepareStatement(sql);
// SQL
statement.setInt(1, 1);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
System.out.println("UserName = " + resultSet.getString("UserName"));
System.out.println("PassWord = " + resultSet.getString("PassWord"));
System.out.println("UserAge = " + resultSet.getInt("UserAge"));
String userSex = resultSet.getInt("UserSex") == 1 ? " " : " ";
System.out.println("UserSex = " + userSex);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
System.out.println(" ");
} finally {
closeConnection(connection);
}
}
}