Java读取txt文件内容。可以作如下理解:
-
首先获得一个文件句柄。File file = new File();file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。
-
通过这条线路读取甲方的信息:new FileInputStream(file)目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西
-
既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据
-
解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。
package taxidata;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class ReadFile {
}
JAVA连接数据库 类
package taxidata;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BeyondbConnection {
public static Connection getConnection() {
Connection con = null;
String url = "jdbc:beyondb://localhost:II7/store ";
String user = "suo";
String password = "123456";
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException ex) {
Logger.getLogger(BeyondbConnection.class.getName()).log(Level.SEVERE,null, ex);
}
return con;
}
}
InfoBean类创建 类
package taxidata;
public class InfoBean {
String license_num;
String recordtime;
int set_num;
int speed;
Double elevation;
String location;
public InfoBean() {
}
public String getLicense_num() {
return license_num;
}
public void setLicense_num(String license_num) {
this.license_num = license_num;
}
public String getRecordtime() {
return recordtime;
}
public void setRecordtime(String recordtime) {
this.recordtime = recordtime;
}
public int getSet_num() {
return set_num;
}
public void setSet_num(int set_num) {
this.set_num = set_num;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Double getElevation() {
return elevation;
}
public void setElevation(Double elevation) {
this.elevation = elevation;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
数据库操作(向数据库添加数据) 类
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class OperationDB {
}
测试主函数 类
public class Text {
}
BufferedReader reader =
new
BufferedReader(
new
FileReader(
"test.txt"
));
PrintStream writer =
new
PrintStream(
new
FileOutputStream(
"test_new.txt"
));
String buf;
while
((buf=reader.readLine()) !=
null
) {
if
(buf.isEmpty()) {
continue
;}
if
(buf.matches(
"[/]+.*"
)) {
buf = buf.replaceAll(
"[/]+(.*)"
,
"$1"
);
//去掉前面的/
}
buf = buf.replaceAll(
"\\s+(.*)"
, $
1
);
//去掉前面的空格
writer.println(buf);
}
reader.close();
writer.flush();
writer.close();
if
(buf.isEmpty()) {
continue
;}
buf = buf.replaceAll(
"^\\s*([^\\s]*)"
,
"$1"
);
//这样可能保险一些,防止注释的前面有空格
if
(buf.matches(
"^[/]+.*"
)) {
buf = buf.replaceAll(
"^[/]+(.*)"
,
"$1"
);
//去掉前面的/
}
buf = buf.replaceAll(
"^\\s*([^\\s]*)"
,
"$1"
);
//去掉前面的空格