久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

讀取由制表符分隔的文件并將單詞放入 ArrayList

Read a file separated by tab and put the words in an ArrayList(讀取由制表符分隔的文件并將單詞放入 ArrayList)
本文介紹了讀取由制表符分隔的文件并將單詞放入 ArrayList的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在做一個自學(xué)練習(xí),以幫助我更多地了解 Java,但我被這個問題困住了.我有以下 txt 文件:

I am doing a self learning exercise to help me understand more about Java, but I am stuck at this question. I have the following txt file:

Name  Hobby 
Susy  eat fish 
Anna  gardening
Billy bowling with friends

注意:姓名和愛好用制表符隔開

閱讀所有行并將其放入 arraylist(name,hobby) 的最佳方法是什么.棘手的部分是

What is the best way to read all the line and put it in arraylist(name,hobby). The tricky part is that

eat fish or bowling with friends

有空格,它必須放在一個數(shù)組下,顯然我無法對其進行硬編碼.這是我當(dāng)前的代碼:

has white spaces and it must be put under one array and obviously I cannot hardcode it. Here is my current code:

 public void openFile(){
            try{
                FileInputStream fstream = new    FileInputStream("textfile.txt");
          // use DataInputStream to read binary NOT text
          BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
          ArrayList<String> names = new ArrayList<String>();
          ArrayList<String> hobbies = new ArrayList<String>();
          String lineJustFetched;
          while ((lineJustFetched = br.readLine()) != null)   {
          String[] tokens = lineJustFetched.split(" 	");

我遇到了一個錯誤:

java.lang.StringIndexOutOfBoundsException:字符串索引超出范圍:-1

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

我懷疑計算索引在選項卡上不是很有用.有什么想法嗎?

I suspect counting the index is not very useful on a tab. Any idea?

推薦答案

好的,你需要按照下圖的方法進行:

Alright, you need to do the recipe shown below:

  1. 創(chuàng)建一個BufferedReader
  2. 創(chuàng)建一個ArrayList
  3. 開始將數(shù)據(jù)讀取到名為 lineJustFetchedString 變量中.
  4. 調(diào)用lineJustFetched.split(" ");
  5. 分割String
  6. 迭代生成的 String[].檢查你要進入ArrayList的token是否不是""
  7. 如果沒有,則將單詞添加到 ArrayList
  1. Create a BufferedReader
  2. Create an ArrayList<String>
  3. Start reading data into a String variable named lineJustFetched.
  4. Split the String by calling lineJustFetched.split(" ");
  5. Iterate over the String[] produced. Check if the token you want to enter into the ArrayList is not ""
  6. If not, add the word to the ArrayList

您指定需要根據(jù) 值進行拆分,這樣空格就不會成為問題.

You specify that you need to split based on values so white spaces won't be an issue.

SSCCE

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class WordsInArray {
    public static void main(String[] args) {
        try{
            BufferedReader buf = new BufferedReader(new FileReader("/home/little/Downloads/test"));
            ArrayList<String> words = new ArrayList<>();
            String lineJustFetched = null;
            String[] wordsArray;

            while(true){
                lineJustFetched = buf.readLine();
                if(lineJustFetched == null){  
                    break; 
                }else{
                    wordsArray = lineJustFetched.split("	");
                    for(String each : wordsArray){
                        if(!"".equals(each)){
                            words.add(each);
                        }
                    }
                }
            }

            for(String each : words){
                System.out.println(each);
            }

            buf.close();

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

輸出

John
likes to play tennis
Sherlock
likes to solve crime

這篇關(guān)于讀取由制表符分隔的文件并將單詞放入 ArrayList的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产黄色精品 | 一区二区三区黄色 | 国产美女视频网站 | 在线免费观看黄色片 | 91麻豆国产| 黄视频在线播放 | 日韩小视频 | 国产精品羞羞答答 | 亚洲欧美精品一区二区 | 一区二区网站 | 少妇精品视频 | 97色综合| 国产麻豆一区二区三区 | 在线中文av | 久久视频在线免费观看 | 欧美激情小视频 | 久久久中文字幕 | 免费看毛片的网站 | 久视频在线 | 两性免费视频 | 国产一级片在线 | 午夜激情福利视频 | 久久中文字幕视频 | 国产成人亚洲精品自产在线 | 久久久精品免费 | 综合伊人久久 | 中文字幕一区二区三区乱码 | 日韩欧美一区二区三区久久婷婷 | 免费观看黄色片 | 国产天堂在线 | 99视频在线观看免费 | 久久久久国产精品视频 | 日本成人中文字幕 | 亚洲免费久久 | 日韩精品免费一区二区夜夜嗨 | 可以看毛片的网站 | 日韩精品免费一区二区夜夜嗨 | 国产精品成人一区二区网站软件 | 人人射人人干 | 美女福利视频 | www.日日夜夜 |