加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 本地

java里怎么读取txt文件里的数据

时间:2024-03-01 04:37:50  来源:http://www.gzrxw.net  作者:admin

java里怎么读取txt文件里的数据

// 直接给一个只读一行的吧,呵呵,数字之间以空格来分隔

public static void main(String[] args) throws Exception {

File file = new File(src/test.txt);

double[] a = getLineFromTxt(file, );

for(int i = 0; i< a.length; i++){

System.out.println(a[+i+] = +a[i]);

}

}

test中的数据以空格隔开 12 32 32.02

public static double[] getLineFromTxt(File file, String split) throws Exception{

BufferedReader br = new BufferedReader(new FileReader(file));

String firstLine = br.readLine(); //就读第一行哦

String[] arrs = firstLine.split( );

//将字符数组转为double数组

double[] arr = new double[arrs.length];

for(int i = 0; i< arr.length; i++){

arr[i] = Double.parseDouble(arrs[i]);

}

if(br!= null){

br.close();

br = null;

}

return arr;

}

JAVA读取文件

先读取你所需要的内容然后再像上面哥们的那样存储到本地

读取可以通过正则匹配  public static String HtmltoText(String inputString) {

    String htmlStr = inputString; //含html标签的字符串

    String textStr =;

    java.util.regex.Pattern p_script;

    java.util.regex.Matcher m_script;

    java.util.regex.Pattern p_style;

    java.util.regex.Matcher m_style;

    java.util.regex.Pattern p_html;

    java.util.regex.Matcher m_html;    

    java.util.regex.Pattern p_ba;

    java.util.regex.Matcher m_ba;

    

    try {

    String regEx_script = <[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }

    String regEx_style = <[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }

    String regEx_html = <[^>]+>; //定义HTML标签的正则表达式

    String patternStr = \\s+;

    

    p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);

    m_script = p_script.matcher(htmlStr);

    htmlStr = m_script.replaceAll(); //过滤script标签

    p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);

    m_style = p_style.matcher(htmlStr);

    htmlStr = m_style.replaceAll(); //过滤style标签

    

    p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);

    m_html = p_html.matcher(htmlStr);

    htmlStr = m_html.replaceAll(); //过滤html标签

    

    p_ba = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE);

    m_ba = p_ba.matcher(htmlStr);

    htmlStr = m_ba.replaceAll(); //过滤空格

    

    textStr = htmlStr;

    

    }catch(Exception e) {

    System.err.println(Html2Text: + e.getMessage());

    }    

    return textStr;//返回文本字符串

    }

然后再使用关键流去保存到本地或者数据库也行,保存到数据库时候可根据需要选择使用流或者不使用流

来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
相关文章
    无相关信息
栏目更新
栏目热门