java读取TXT文件类

    xiaoxiao2021-12-14  20

    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 {

        publicstatic void readTxtFile(String filePath) {        InfoBean infoBean=new InfoBean();        try {            String encoding = "GBK";            File file = new File(filePath);            if (file.isFile() && file.exists()){ //判断文件是否存在                InputStreamReader read = new InputStreamReader(                        new FileInputStream(file), encoding);//考虑到编码格式                BufferedReader bufferedReader = new BufferedReader(read);                String lineTxt = null;                 OperationDB operationDB=new OperationDB();                while ((lineTxt = bufferedReader.readLine()) != null) {                    String[] s=lineTxt.split(",");                    infoBean.setLicense_num(s[0].toString());                    infoBean.setRecordtime(s[1].toString());                    infoBean.setSet_num(Integer.valueOf(s[2]));                    infoBean.setSpeed(Integer.valueOf(s[3]));                    infoBean.setElevation(Double.valueOf(s[4]));                    String location="point"+"("+Double.valueOf(s[6])+""+Double.valueOf(s[5])+")";                    infoBean.setLocation(location);                                       operationDB.addRcorder(infoBean);                           System.out.println(lineTxt);                                    }                read.close();            } else {                System.out.println("找不到指定的文件");            }        } catch (Exception e) {            System.out.println("读取文件内容出错");            e.printStackTrace();        }     } }

    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; } }

     

    数据库操作(向数据库添加数据) 类

     package taxidata;

    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 {        private Connection con = null;     public void addRcorder(InfoBean infoBean) throws SQLException{         if(con ==null){              con = BeyondbConnection.getConnection();         }                String sql = "insert into taxidatavalues(?,?,?,?,?,st_geomfromtext(?))";                  PreparedStatement pstmt = con.prepareStatement(sql);            pstmt.setString(1, infoBean.getLicense_num());            pstmt.setString(2, infoBean.getRecordtime());            pstmt.setInt(3, infoBean.getSet_num());            pstmt.setInt(4, infoBean.getSpeed());            pstmt.setDouble(5, infoBean.getElevation());            pstmt.setString(6,infoBean.getLocation());                        pstmt.executeUpdate();        } }

    测试主函数 类

     package taxidata;

    public class Text {

            publicstatic void main(String[] args) {        String filePath = "C:\\GPS_2012_07_01.TXT";        ReadFile readFile=newReadFile();          readFile.readTxtFile(filePath);     } }

     

     //过滤txt中的空行等符号

     

    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" );  //去掉前面的空格
    转载请注明原文地址: https://ju.6miu.com/read-962407.html

    最新回复(0)