2016-11-09 10 views
0

私が書いたコードは.CSVファイルではなく、.txtだけで動作します。私の.CSVファイルに書き込めません

私のコードの目的は、field1からユーザー入力を受け取り、ファイル内にユーザー入力のインスタンスがあるかどうかを確認するために.CSVファイルをチェックすることです。存在する場合、フィールド2からのユーザー入力に置き換えられます。

これは私の.txtファイルでは動作しますが、.CSVファイルでは動作しません。ここで

は、ボタンを押すだけで活性化されたコードは(ボタンを保存)です:

try{ 
    // Input the file location into Path variable 'p' 
    //Cannot write to CSV files 
    //Path p = Paths.get("C:\\Users\\myname\\Documents\\Stock Take Program\\tiger.csv"); 

    Path p = Paths.get("C:\\Users\\myname\\Desktop\\test.txt"); 

    //Read the whole file to a ArrayList 
    List<String> fileContent = new ArrayList<>(Files.readAllLines(p)); 

    //Converting user input from editSerialField to a string 
    String strSerial = editSerialField.getText(); 
    //Converting user input from editLocationField to a string 
    String strLocation = editLocationField.getText(); 

    //This structure looks for a duplicate in the text file, if so, replaces it with the user input from editLocationField. 
    for (int i = 0; i < fileContent.size(); i++) 
    { 
     if (fileContent.get(i).equals(strSerial)) 
     { 
      fileContent.set(i, strLocation); 
     } 
     break; 
    } 

    // write the new String with the replaced line OVER the same file 
    Files.write(p, fileContent); 

    }catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 

私の質問はどのように私は.CSVの内容を更新し、交換に対応するためにコードを更新することができ、あります私の.txtファイルで動作するのと同じ方法で、ユーザー入力を使用してファイルを作成します。

テキストファイルに書き込むときは、最初の行だけが置換されますが、.CSVファイルに書き込むときには何も置き換えられません。

とにかく、.CSVファイル内のテキストを置き換えるためにコードを別に書く必要がありますか。

ご協力いただきありがとうございます。

+0

csvファイルは、パスにスペースのない場所にある場合はどうなりますか? –

+0

それは違いはありません。 – juiceb0xk

+0

タイトルに「ファイルが見つかりません」と表示されているので、私は混乱しています –

答えて

0

私はばかです。私の '.CSV'ファイルは実際にはtiger.csvというタイトルのテキストファイルです。私は今、実際のCSVバージョンを保存して、今働いています。

ハハ、助けてくれてありがとう。

+0

ただし、.CSVファイルへの書き込みはまったく行われません。私は何かを置き換えることはできません。 私は今質問と私の問題を更新しました。 – juiceb0xk

0

おそらく別の質問になるはずですが、最初の行でのみ動作する変更に関する問題は、breakが呼び出され、最初の方法でループがショートカットされるためです。それをブロックifに入れてください。

for (int i = 0; i < fileContent.size(); i++) 
{ 
    if (fileContent.get(i).equals(strSerial)) 
    { 
     fileContent.set(i, strLocation); 
     break; 
    } 
} 

複数の行を更新できるようにする場合は、完全にオフにしてください。

0

HTH、

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

      String UserEntredValu="Karnataka"; 
      String csvFile = "C:/Users/GOOGLE/Desktop/sample/temp.csv"; 
      String line = ""; 
      String cvsSplitBy = ","; 
      PrintWriter pw = null; 
      pw = new PrintWriter(new File(csvFile)); 
      try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) { 

       while ((line = br.readLine()) != null) { 

        // use comma as separator 
        String[] country = line.split(cvsSplitBy); 

        for(int i = 0; i < country.length - 1; i++) 
        { 
         String element = country[i]; 

         if(element.contains(UserEntredValu)){ 
          String newEle=element.replace(element, "NEW INDIA"); 
          pw.write(newEle); 
          System.out.println("done!"); 
          pw.close(); 
         } 
        } 
      } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
関連する問題