zoukankan      html  css  js  c++  java
  • 【Java】 读取Txt文件 处理数据

    @RequestMapping("/importTxt")
        public String readTxtPoints(@RequestParam("file") MultipartFile file){
            String encoding="GBK";
            InputStream in = null;
            if (file.isEmpty()) {
                putInRequest("error", "文件不存在");
            }else{
                try {
                    in = file.getInputStream();
                    InputStreamReader read = new InputStreamReader(in,encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    int i=0;
                    String[] ss=null;
                    StringBuilder boundaryPoints=new StringBuilder();
                    while((lineTxt = bufferedReader.readLine()) != null){
                        if(i!=0){
                            ss=lineTxt.split("\,",-1);
                            boundaryPoints.append(ss[0]+","+ss[2]+","+ss[1]+";");
                            
                        }
                        i++;
                    }
                    if(boundaryPoints.length()>0){
                        boundaryPoints.deleteCharAt(boundaryPoints.length()-1);
                    }
                    System.out.print(boundaryPoints);               
                    read.close();
                    putInRequest("boundaryPoints", boundaryPoints);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return "land/importPoints";
        }
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    	<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%
    	String path = request.getContextPath();
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>导入经纬度</title>
    <jsp:include page="/common.jsp"></jsp:include>
    <script type="text/javascript">
    function check() {  
        var file = $("#file").val();  
        if (file == "" || file.length == 0) {  
            alert("请选择文件路径!");  
            return false;  
        } else {  
        	document.getElementById('sum').click();
           return true;  
        }  
    } 
    </script>
    </head>
    <body>
    	<form action="<%=path%>/land/importTxt"  method="post" enctype="multipart/form-data">
    		<textarea name="boundaryPoints" id="boundaryPoints" cols="85" rows="10" >${boundaryPoints}</textarea>		
    		<input
    			id="file" name="file" type="file" value="" 
    			onchange="document.getElementById('textfield').value=this.value;" />
    		<input type="submit" id="sum"
    			style="display: none;" />
    			<input type="button" class="i_button marie" value="导入"
    			onclick=" check()" /> 
    	</form>
    </body>
    </html>
    

      格式和数据处理自己定义

    效果:

  • 相关阅读:
    HAOI2008题解
    codeforces round375(div.2)题解
    codeforces round373(div.2) 题解
    TJOI2015题解
    CF976D. Degree Set
    dtoj#4243. 熊猫(i)
    dtoj#4242. 大爷(w)&&CF1061E
    CF786C Till I Collapse
    dtoj#4239. 删边(cip)
    dtoj#2504. ZCC loves cube(cube)
  • 原文地址:https://www.cnblogs.com/zwqh/p/6774892.html
Copyright © 2011-2022 走看看