1. 程式人生 > >java處理txt檔案,並生成檔案

java處理txt檔案,並生成檔案

@Test
	public void transfer() throws IOException{
		String tx = null;
		FileInputStream fis = new FileInputStream("DDDSA2005.TXT");
		FileOutputStream fos = new FileOutputStream("newDDDSA2005.CSV");
		InputStreamReader read = new InputStreamReader(fis, "UTF-8");
		BufferedReader br = new BufferedReader(read);
		String line = System.getProperty("line.separator");
		while ((tx = br.readLine()) != null) {
            String newtx = 
              tx.substring(0, 19) + "|" 
		    + tx.substring(21, 29) + "|"			
			+ tx.substring(31, 39) + "|"			
			+ tx.substring(39, 43) + "|"			
			+ tx.substring(43, 57) + "|"			
			+ tx.substring(58, 71) + "|"
			+ tx.substring(71, 76) + "|"
			+ tx.substring(76, 77) + "|"
			+ tx.substring(77, 78) + "|"
			+ tx.substring(78, 79) + "|"
			+ tx.substring(79, 80) + "|"
			+ tx.substring(82, 97) + "|"
			+ tx.substring(99, 114) + "|"
			+ tx.substring(115, 124) + "|"
			+ tx.substring(126, 132) + "|"
			+ tx.substring(132, 138) + "|"
			+ tx.substring(138, 144) + "|"
			+ tx.substring(144, 154) + "|"
			+ tx.substring(154, 164) + "|"
			+ tx.substring(166, 172) + "|"
			+ tx.substring(172, 173);
            
            fos.write(newtx.getBytes());
            fos.write(line.getBytes());
        }

實現按行處理,每行內容按格式加|分隔符。