【问题标题】:How to load a txt.file into an existing object?如何将 txt.file 加载到现有对象中?
【发布时间】:2016-08-06 15:36:02
【问题描述】:

过去几天我一直在尝试这样做。我已经搜索并完成了它的副本,但它们都不起作用,我尝试了简单的加载。但无论如何它们都不起作用。

public class Data
{
String Name;
 int Points;
 int Heart, Next;

 public Data(String N,int A,int V,int P)
  
{     Name = N;
   Next = A;
   Heart = V;
  Points = P;

   }
 }

我这样保存它们

bw.write(Dat[j].Name + " " + Dat[j].Next + "" + Dat[j].Heart + " " + Dat[j].Points);

之后有一个函数可以保存对象的数据并将其放置为文本文件,即“Players.txt”,结果如下 例如:

赫克托 0 3 0

玛丽 0 3 0

约翰 0 3 0

维克多 0 3 0

现在这是一个 Data 数组,即 Data Dat[] = new Data [Max];并且每次都有一个“J”计数器,就像一个简单的对象。程序启动时我想要的只是加载文本文件中的整个数据并将其作为这个对象。

    public static void Scoreboard() 
   { Loading(); // this suppose to be the function to load the data
        f(j > 0)
            {   
        for(int i=0; i<j; i++)
                {   
                    
    Dat[j].Show("Number"+(j+1)); // .Show is a function in the object Data    
    
                 }//for
          
            }//if
            else System.out.print("No users");
            return;
        }// Scoreboard

我对java很陌生,我真的不了解缓冲的读写器,我会提前感谢一些帮助。

【问题讨论】:

    标签: java object text bufferedreader bufferedwriter


    【解决方案1】:

    快速更新我自己做的谁想知道这里是我的代码。 (西班牙语)

     public static void Loading()
           {
    
    String Nombre;
    int Vida;
    int Puntuacion;
    int Aprobado;
    File f = new File(ruta);
            try
            {   /*Si existe el fichero*/
                if(f.exists())
                {   /*Abre un flujo de lectura a el fichero*/
                    BufferedReader Flee= new BufferedReader(new FileReader("Jugadores.txt"));
                    String str=null;
                    /*Lee el fichero linea a linea hasta llegar a la ultima*/
                    ArrayList<String> lines = new ArrayList<String>();
                    while((str=Flee.readLine())!=null)
                    {   /*Imprime la linea leida*/
                        lines.add(str);
                        String[] linesArray = lines.toArray(new String[lines.size()]);
                    }//WHILE
                    /*Cierra el flujo*/
    
                    for(int j=0; j<=lines.size();j = j + 4){
                     Nombre= lines.get(j);
                     Aprobado= Integer.parseInt(lines.get(j+1));
                     Vida=  Integer.parseInt(lines.get(j+2));
                     Puntuacion= Integer.parseInt(lines.get(j+3));
    
                    Dat[cp] = new Datos(Nombre,Aprobado,Vida,Puntuacion);
                    cp ++;
                    }
    
    
    
    
    
                    Flee.close();
                }
                else
                    System.out.println("Fichero No Existe");
            }//try
            catch (Exception ex)
            {   /*Captura un posible error y le imprime en pantalla*/
                System.out.println(ex.getMessage());
            }//catch
    }
    

    我把它逐行变成了一个 ArrayList,当然,你需要你的文本文件像这样: 赫克托

    3

    0

    3

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2015-05-17
      • 2023-03-13
      • 1970-01-01
      • 2021-12-27
      • 2018-10-05
      • 1970-01-01
      相关资源
      最近更新 更多