文件和程序的部分输出不是来自迭代

时间:2015-06-10 07:08:03

标签: java

我在阅读文件内容时遇到了一些问题。情节就是这样 文件示例:

Australia____06092015_224554    
Baltic____06092015_131331   
Belux____06102015_000530    
Bulgaria____06092015_121454 
Chile____06092015_124625    
Colombia____06092015_123502 
Croatia____06092015_123520  
Germany____06102015_004530  
HongKong____06102015_030619 
Italy____06092015_134508    
Korea____06092015_212856    
Malaysia____06092015_230835 
Morocco____06092015_163055  
Netherlands____06092015_175635  
Poland____06092015_174550   
Romania____06092015_150912  
Russia____06102015_000734   
Serbia____06092015_153549   
Singapore____06102015_000841    
South Africa Sub Sahara 2____06092015_194840    
Spain____06092015_113544    
Thailand____06102015_110733     
Turkey____06102015_041318   
Ukraine____06102015_003746  
Uruguay____06092015_152429  
Vietnam____06102015_020824  

但输出是:

Australia

Baltic

Belux

Bulgaria

Chile

Colombia

Croatia

Germany

HongKong

Italy

Korea

Malaysia

Morocco

Netherlands

Poland

Romania

Russia

Serbia

Singapore

SouthAfricaSubSahara

Spain

Thailand

Turkey

虽然总输入文件行是26但是只有23个在输出中被访问,直到字符串“土耳其”,我的程序甚至没有出现在cmd中。所以我想它会被String“土耳其”击中,但不知道为什么呢?

import java.sql.*;
import java.io.*;
public class tst {

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

        FileInputStream fin;
        int k = 0;
        String line, s, g, d;
        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");

            //DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());

            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.195.100:1521:ROW0", "azglobal_users_prd", "catcat1");

            Statement stmt = conn.createStatement();

            fin = new FileInputStream("C:/Users/BOT2/Desktop/OGL/MC_WIth_DATA_Files.txt");

            DataInputStream in1 = new DataInputStream(fin);

            BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));


            while ((line = br1.readLine()) != null) {
                k++;
                s = line.replaceAll("[0-9_]+", "");

                g = s.replaceAll("\\s+", "");

                System.out.println(g);

                d = "update ogl_table_status set status = 1 where mc_name='" + g + "'";
                stmt.executeUpdate(d);
            }

            System.out.println(k);
            stmt.close();
            br1.close();
            fin.close();

        }
        // Catches any error conditions
        catch (IOException e) {
            System.err.println("Unable to read from file");
            System.exit(-1);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果可以的话,

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(
                "C:/MC_WIth_DATA_Files.txt"));
        String line = null;
        while ((line = br.readLine()) != null) {
            int idx = line.indexOf('_');
            String nation = idx > 0?line.substring(0, idx):line;
            System.out.println(nation);
        }
        br.close();
    }

}