简体   繁体   English

如何从txt文件中排序字符串并将其发布到textview项目

[英]How to sort strings from txt file and post them to textview item

I am reading from txt file some strings.TextView shows me:"score=10" "score=1""score=5""score=11" I want to sort this like:TextView shows:"score=1" "score=5""score=10""score=11" I dont know how to sort this strings.this are not ints. 我正在从txt文件读取一些string.TextView显示给我:“ score = 10”“ score = 1”“ score = 5”“ score = 11”我想像这样进行排序:TextView显示:“ score = 1”“ score = 5“”得分= 10“”得分= 11“我不知道如何对这些字符串进行排序。这不是整数。 Can u Help me with an example ? 你能帮我举个例子吗? Or can u see my code? 还是可以看到我的代码?

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wyniki);

    final TextView scores = (TextView) findViewById(R.id.textView4);
    final Button button5 = (Button) findViewById(R.id.button5);
    String saved_scores = readText();
    if (saved_scores.length()>0){
        scores.setText(saved_scores);}

    sort();

    button5.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {


             final File f = new File(Environment.getExternalStorageDirectory()+"/cos/Wynik.txt");
            if (f.exists()){
                f.delete(); 

                Toast.makeText(getApplicationContext(),"Deleted",Toast.LENGTH_SHORT).show();


                scores.setText(null);

            }
        }
    });


}

public String readText(){
     //this is your text
     StringBuilder text = new StringBuilder();
 try{

     final File f = new File(Environment.getExternalStorageDirectory()+"/cos/Wynik.txt");
     FileInputStream fileIS = new FileInputStream(f);
     BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
              String readString = "";
     //just reading each line and pass it on the debugger  
     while((readString = buf.readLine())!= null){
        Log.d("line: ", readString);
        text.append(readString + "\n");
               }
     buf.close();
  } catch (FileNotFoundException e) {

     e.printStackTrace();

  } catch (IOException e){

     e.printStackTrace();

  }
         return text.toString();

} }

Remove the "score=" part of each line in the text file, and for each line check if the remaining content is a valid number - if so, store that as an integer in a collection. 删除文本文件中每一行的“ score =”部分,然后针对每一行检查其余内容是否为有效数字-如果是这样,请将其作为整数存储在集合中。 Once you're done with reading the text file, you can just sort the collection and use the data in whatever fashion you want in your TextView. 阅读完文本文件后,您可以对集合进行排序,并以所需的方式在TextView中使用数据。

You can do the following Scanner Reader=new Scanner(fileName); 您可以执行以下Scanner Reader = new Scanner(fileName); In a for loop read all the integers from using Reader.nextInt() function. 在for循环中,使用Reader.nextInt()函数读取所有整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从.txt文件中读取字符串,然后根据出现的次数将它们分类到ArrayList中? - How to read strings off of .txt file and sort them into an ArrayList based on the number of occurrences? 以2个字符串传递值并将其从.txt文件中删除 - Passing values in 2 strings and removing them from a .txt file 如何从.txt文件获取多个字符串 - How to get multiple Strings from .txt file 如何从 JTable txt 文件创建字符串? - How to create Strings from a JTable txt file? 如何从ListView onClickItem在TextView中显示.txt文件? - How to display .txt file in a TextView from ListView onClickItem? 如何以精确的格式将 txt 文件从 url 解析为 textview? - How to parse a txt file from a url in the exact format to a textview? 从外部.txt文件中流式传输大字符串还是直接将其直接编码为更聪明? - Is it smarter to stream in large strings from an external .txt file, or to simply code them directly in? Java:如何对txt文件进行排序? - Java: How to sort the txt file? 如何使用扫描仪输入读取带有斜杠“/”的 txt 文件并将它们放入数组/字符串中 - How to read txt file with slashes “/” with scanner input and put them into arrays/strings 如何对从JAVA中的.txt文件读取的数组进行排序 - How to sort an Array that is read from a .txt file in JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM