将文本文件中的url添加到字符串数组(Android)

时间:2014-08-14 19:52:33

标签: android streamreader filereader arrays

我正在处理图像库应用程序,其中我从互联网上加载图像,所以我在文本文件中添加了许多图像URL,现在我想将这些URL从文本文件添加到字符串数组。

我不知道如何将文件中的文字添加到我的代码中(public static final String[] IMAGES = new String[] {};

这是我的代码示例:

在此代码中我手动添加了URl,但我想将文本文件中的URL添加到public static final String[] IMAGES = new String[]{};

public final class Constants {

    public static final String[] IMAGES = new String[] {                        
            // Light images
            "http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png",
            "http://simpozia.com/pages/images/stories/windows-icon.png",
            "http://radiotray.sourceforge.net/radio.png",
            "http://www.bandwidthblog.com/wp-content/uploads/2011/11/twitter-logo.png",
            "http://weloveicons.s3.amazonaws.com/icons/100907_itunes1.png",
            "http://weloveicons.s3.amazonaws.com/icons/100929_applications.png",
            "http://www.idyllicmusic.com/index_files/get_apple-iphone.png",
            "http://www.frenchrevolutionfood.com/wp-content/uploads/2009/04/Twitter-Bird.png",
            "http://3.bp.blogspot.com/-ka5MiRGJ_S4/TdD9OoF6bmI/AAAAAAAAE8k/7ydKtptUtSg/s1600/Google_Sky%2BMaps_Android.png",
            "http://www.desiredsoft.com/images/icon_webhosting.png",
            "http://goodereader.com/apps/wp-content/uploads/downloads/thumbnails/2012/01/hi-256-0-99dda8c730196ab93c67f0659d5b8489abdeb977.png",
            "http://1.bp.blogspot.com/-mlaJ4p_3rBU/TdD9OWxN8II/AAAAAAAAE8U/xyynWwr3_4Q/s1600/antivitus_free.png",
            "http://cdn3.iconfinder.com/data/icons/transformers/computer.png",
            "http://cdn.geekwire.com/wp-content/uploads/2011/04/firefox.png?7794fe",
            "https://ssl.gstatic.com/android/market/com.rovio.angrybirdsseasons/hi-256-9-347dae230614238a639d21508ae492302340b2ba",
            "http://androidblaze.com/wp-content/uploads/2011/12/tablet-pc-256x256.jpg",
            "http://www.theblaze.com/wp-content/uploads/2011/08/Apple.png",
            "http://1.bp.blogspot.com/-y-HQwQ4Kuu0/TdD9_iKIY7I/AAAAAAAAE88/3G4xiclDZD0/s1600/Twitter_Android.png",
            "http://3.bp.blogspot.com/-nAf4IMJGpc8/TdD9OGNUHHI/AAAAAAAAE8E/VM9yU_lIgZ4/s1600/Adobe%2BReader_Android.png",
            "http://cdn.geekwire.com/wp-content/uploads/2011/05/oovoo-android.png?7794fe",
            "http://icons.iconarchive.com/icons/kocco/ndroid/128/android-market-2-icon.png",
            "http://thecustomizewindows.com/wp-content/uploads/2011/11/Nicest-Android-Live-Wallpapers.png",
            "http://c.wrzuta.pl/wm16596/a32f1a47002ab3a949afeb4f",
            "http://macprovid.vo.llnwd.net/o43/hub/media/1090/6882/01_headline_Muse.jpg",           
    };

    private Constants() {
    }

    public static class Config {
        public static final boolean DEVELOPER_MODE = false;
    }

    public static class Extra {
        public static final String IMAGES = "com.nostra13.example.universalimageloader.IMAGES";
        public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";
    }
}

2 个答案:

答案 0 :(得分:1)

好的,没问题。

我建议您使用列表而不是数组,因为数组具有固定大小,而arraylist具有无限大小。

public final class Constants {

   public static List<String> IMAGES = new ArrayList<String>();

   private Constants() {
   }

   public static class Config {
       public static final boolean DEVELOPER_MODE = false;
   }



   public static class Extra {
        public static final String IMAGES = "com.nostra13.example.universalimageloader.IMAGES";
       public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";
    }

    public void parseFile(){
      try {

          String sCurrentLine;

          br = new BufferedReader(new FileReader("C:\\myFile.txt"));

          while ((sCurrentLine = br.readLine()) != null) {
              IMAGES.add(sCurrentLine);
          }
          br.close();
      } 
      catch (IOException e) {
         e.printStackTrace();
      } 
      finally {
          try {
              if (br != null)br.close();
          } 
          catch (IOException ex) {
                   ex.printStackTrace();
          }
       }
     }
}

答案 1 :(得分:-1)

这是解释您想要的内容的链接:http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("C:\\myFile.txt"));

        int i = 0;

        while ((sCurrentLine = br.readLine()) != null) {
              IMAGES[i] = sCurrentLine;
              i++;
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }