zoukankan      html  css  js  c++  java
  • Java 阅读TXT文件

    public class GenCategoryAttrItemHandler {
    
    	private final static String INPUT_FILE_PATH = "input/category_attr_item.txt";
    	private final static String OUTPUT_FLIE_PATH = "output/category_attr_itemList.txt";
    	
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		GetDataFromFile();
    		WriteDataToFile();
    		System.out.println("ok");
    	}
    	
    	/**
    	 * 从文件提取数据。格式化到程序中
    	 * @throws IOException
    	 */
    	public static void GetDataFromFile() throws IOException {
    		final String DELIMITER = "	";
    		
    		BufferedReader br = null;
    		try {
    			File file = new File(INPUT_FILE_PATH);
    			br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    			
    			String line = null;
    			while (null != (line=br.readLine())) {
    				String[] strs = line.split(DELIMITER);
    				Category category = new Category(Long.parseLong(strs[0]),strs[1]);
    				Attribute attribute = new Attribute(Long.parseLong(strs[2]),strs[3]);
    				Item item = new Item(Long.parseLong(strs[4]),strs[5]);
    				...
    			}// while
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			if (null != br) {
    				br.close();
    			}
    		}
    	}
    
    	/**
    	 * 将格式化的数据写入文件里
    	 * @throws IOException 
    	 */
    	public static void WriteDataToFile() throws IOException {
    		final String DELIMITER = "	";
    		
    		BufferedWriter bw = null;
    		try {
    			File file = new File(OUTPUT_FLIE_PATH);
    			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
    			
    			for (int i=1; i<12; ++i) {
                                    ...
    				bw.write(outValue);  // 将数据写入文件里
    				bw.newLine();        // 新建一个换行符
    				bw.flush();
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			if (null != bw) {
    				bw.close();
    			}
    		}
    	}
    }

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Notepad++可以结合命令行来搭建各种编程环境
    installshield中如何执行批处理 还有设置环境变量
    intallshield各种控制语句
    实际应用中installshield的事件处理
    intallshield各种控制语句
    java 沙箱
    手写installshield入门
    installshield安装程序自注册dll
    实际应用中installshield的事件处理
    installshield中如何执行批处理 还有设置环境变量
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4660554.html
Copyright © 2011-2022 走看看