java - 如何通过读取 .txt 文件分配变量并将其设置为固定长度?

标签 java arraylist string-formatting

我正在尝试读取一个不固定长度的 .txt 文件并将其转换为某个固定长度。我尝试在 while 循环中为读取为 split() 的每一行使用一个数组,但它一直给我奇怪的格式,所以我把它拿出来了!我希望该机构的格式为 40 个字符长度,v_25 - 子变量的固定长度为 3,注册变量设置为 4!请帮忙!

import java.io.*;

public class FileData {


public static void main (String[] args) {

   File file = new File("test.txt");
   StringBuffer contents = new StringBuffer();
   BufferedReader reader = null;
  // int counter = 0;
   String institution = null;
   String V_25 = null; 
   String V_75 = null;
   String M_25 = null;
   String M_75 = null;
   String Submit = null;
   String Enrollment = null;

   try {
       reader = new BufferedReader(new FileReader("/Users/GANGSTATOP/Documents/workspace/DBTruncate/src/input.txt"));               
       String text = null;

    // repeat until all lines is read
    while ((text = reader.readLine()) != null) {
        contents.append(text.replaceAll(",", " ")).append("\nblank\n");

         }


   } catch (FileNotFoundException e) {
            e.printStackTrace();
   } catch (IOException e) {
            e.printStackTrace();
   } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
     } catch (IOException e) {
                e.printStackTrace();
            }
 }

        // show file contents here  

        System.out.println(contents.toString());
  }


 }

最初读取文件:

   Adelphi University,500,600,510,620,715,7610
   Alabama State University,380,470,380,480,272,5519 

我如何努力让它看起来像:

  (institution)                (v_25)  (v_75)   (m_25)  (m_75) (sub) (enroll)
  Adelphi University           500     600      510     620    715    7610
  blank
  Alabama State University     380     470      380     480    272    5519
  blank

最佳答案

这是我的建议:

BufferedReader reader = null;
List<String> list = new ArrayList<>();
try {
    reader = new BufferedReader(new FileReader("input.txt"));
    String temp;
    while ((temp= reader.readLine()) != null) { 
        list.add(temp); 
    }
}
catch (FileNotFoundException e) { e.printStackTrace(); } 
catch (IOException e) { e.printStackTrace(); }
finally {
    try { if (reader != null) { reader.close(); } } 
    catch (IOException e) { e.printStackTrace(); }
}
System.out.println(String.format("%12s%12s%12s%12s%12s%12s\n\n", 
    "v_25", "v_72", "m_25", "m_75", "Sub", "Enroll"));
for(int i= 0;i < list.size();i++){
    String temp2[] = list.split(",");
    if(temp2.length == 6){
        System.out.println(String.format("%12s%12s%12s%12s%12s%12s\n\n", temp2[0], temp2[1],temp2[2],temp2[3],temp2[4],temp2[5]);
    }
    else{ 
        System.out.println("Error");
    }
}

这将是我的答案初稿。

关于java - 如何通过读取 .txt 文件分配变量并将其设置为固定长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41913517/

相关文章:

java URL在浏览器中有效,但在客户端程序中无效

java - 比较 ArrayList Java 中的部分对象

java - 使用 GSON,是否可以获取 HashMap 中的字段列表

python - 为什么字符串变量在更改时不更新

php - 防止 PHP 解释源代码中字符串中的新行

java - 一个测试类中有多种部署方法

java - Adobe CQ5 - 从外部数据库填充下拉列表

Java - 对 ArrayList 进行排序

java - Integer.parseInt 返回错误 : The method parseInt(String) in the type Integer is not applicable for the arguments (R. 字符串)

Python Format string "}"填充