java中关于文件流的读写(Writer Reader)
1个回答
展开全部
你好 我刚刚做了个例子 方便你看
class_writer class 这个例子是从 一个文件中读取数据然后插入了数据库中 你可以只看读取与插入的过程 希望能帮到你.
public class writer {
public boolean writ(String str) {
boolean success=false;
str = str.replaceAll(" ", "").replaceAll("\"", "");
String[] strs = str.split(",");
BaseJDBC base = new BaseJDBC();
String ML = "";
Statement stmt;
Connection conn;
ML = "'"+strs[0].replace(" ", "").trim() + "','"
+ strs[1].replace(" ", "").trim() + "','"
+ strs[2].replace(" ", "").trim() + "','"
+ strs[3].replace(" ", "").trim() + "','"
+ strs[4].replace(" ", "").trim()+ "','"
+ strs[5].replace(" ", "").trim()+"','"
+ strs[6].replace(" ", "").trim()+"'";
String query = "INSERT INTO BANK_INFO VALUES(" + ML + ")";
if (!strs[0].equals("参与者行号")) {
try {
conn=base.genConn();
stmt = conn.createStatement();
int num=stmt.executeUpdate(query);
if(num==1)success=true;
stmt.close();
conn.close();
System.out.println();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
return success;
}
//class readingclass
public class reading {
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
writer w=new writer();
try {
tempString.getBytes("utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(!w.writ(tempString)){
System.out.println("第"+line+"行出现异常:"+tempString);
}else{
System.out.println("第"+line+"行初始化成功!");
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//1927
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:\\BankInfo_20110714094211.csv";
readFileByLines(fileName);
}
class_writer class 这个例子是从 一个文件中读取数据然后插入了数据库中 你可以只看读取与插入的过程 希望能帮到你.
public class writer {
public boolean writ(String str) {
boolean success=false;
str = str.replaceAll(" ", "").replaceAll("\"", "");
String[] strs = str.split(",");
BaseJDBC base = new BaseJDBC();
String ML = "";
Statement stmt;
Connection conn;
ML = "'"+strs[0].replace(" ", "").trim() + "','"
+ strs[1].replace(" ", "").trim() + "','"
+ strs[2].replace(" ", "").trim() + "','"
+ strs[3].replace(" ", "").trim() + "','"
+ strs[4].replace(" ", "").trim()+ "','"
+ strs[5].replace(" ", "").trim()+"','"
+ strs[6].replace(" ", "").trim()+"'";
String query = "INSERT INTO BANK_INFO VALUES(" + ML + ")";
if (!strs[0].equals("参与者行号")) {
try {
conn=base.genConn();
stmt = conn.createStatement();
int num=stmt.executeUpdate(query);
if(num==1)success=true;
stmt.close();
conn.close();
System.out.println();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
}
return success;
}
//class readingclass
public class reading {
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
writer w=new writer();
try {
tempString.getBytes("utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(!w.writ(tempString)){
System.out.println("第"+line+"行出现异常:"+tempString);
}else{
System.out.println("第"+line+"行初始化成功!");
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//1927
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:\\BankInfo_20110714094211.csv";
readFileByLines(fileName);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询