在Java中操纵一个文本文件? [英] Manipulating a text file in Java?

查看:111
本文介绍了在Java中操纵一个文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个程序,读取外部txt文件,并操纵它。该文件有数据的5个不同的组,每组4线(2 INT是,2串)。我需要使用扫描仪类读取文件,做一个对象来保存每组数据(写入存储数据组作为一个对象的类(让称它为ProgramData))。然后,我需要创建一个ProgamData对象,并将它放入一个ArrayList,并重复为每个5组。

我有一个文本文件,和我一起扫描仪读取它(我证实,我没有通过印刷在命令行上这一权利)。我完全从那里消失。任何帮助都将大大AP preciated。

不喜欢这个会有帮助,但这里是我的code迄今:

 进口java.io. *;
进口的java.util.ArrayList;
进口java.util.Scanner中;公共类PROJECT1
{
    公共静态无效的主要(字串[] args)抛出IOException异常
    {        文件数据文件=新的文件(C:\\\\用户/ data.txt中);
        扫描仪的FileReader =新的扫描仪(数据文件);        INT firstLine中= fileReader.nextInt();
        INT secondLine = fileReader.nextInt();
        串空白符= fileReader.nextLine();
        串thirdLine = fileReader.nextLine();
        串fourthLine = fileReader.nextLine();        ArrayList的<串GT; newArray =新的ArrayList<串GT;();    }
}


解决方案

下面是您可以遵循的步骤:


  1. 创建一个名为ProgramData类

  2. 请一个构造函数,接受您的组的数据。 - >
    什么是构造

  3. 现在在PROJECT1类正确读取文件。 - > 扫描仪教程 使用扫描仪的Java

    阅读txt文件

  4. 一旦你得到所有从文件中的第一组数据把它传递给ProgramData类并创建实例,像

      ProgramData PD1 =新ProgramData(/ * *参数列表/)


  5. 添加了ProgramData instace像下面的ArrayList

      //定义Arraylilst
     ArrayList的< ProgramData>名单=新的ArrayList< ProgramData>(); //不喜欢阅读或收集数据和创建对象的一些操作
     //在步骤4中示出 list.add(PD1); //增加一批新的对象列出。


我希望这将帮助你实现你的目标。如果您有任何问题,只是问。祝你好运

I'm trying to make a program that reads in an external .txt file and manipulates it. The file has 5 different groups of data, 4 lines each (2 are int, 2 string). I need to read in the file using the Scanner class, Make an object to hold each group of data (write a class which stores the data group as a single object (lets call it ProgramData)). Then I need to create a ProgamData object and put that into an ArrayList, and repeat for each of the 5 groups.

I have a text file, and I read it in with the Scanner (I confirmed that I did this right through printing on the command line). I'm completely lost from there. Any help at all would be greatly appreciated.

Not like this will help, but here's my code so far:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Project1
{
    public static void main (String[] args) throws IOException 
    {

        File dataFile = new File("C:\\Users/data.txt");
        Scanner fileReader = new Scanner(dataFile);

        int firstLine = fileReader.nextInt();
        int secondLine = fileReader.nextInt();
        String whiteSpace = fileReader.nextLine();
        String thirdLine = fileReader.nextLine();
        String fourthLine = fileReader.nextLine();

        ArrayList<String> newArray = new ArrayList<String>();

    }


}   

解决方案

Here are the steps you can follow:

  1. Create a class named as ProgramData
  2. Make a constructor which will accept your group data. --> What is constructor
  3. Now in Project1 Class read the file properly. --> Scanner Tutorial and Reading a txt file using scanner java

  4. Once you get all the first group data from file pass it to ProgramData class and create instance something like

      ProgramData pd1 = new ProgramData (/* list of parameter */)
    

  5. Add that ProgramData instace to Arraylist like below

     //  Define Arraylilst 
     ArrayList<ProgramData > list= new ArrayList<ProgramData >();
    
     // Do some operation like reading or collecting the data and creating object 
     // shown in step 4
    
     list.add(pd1); // add new object of group to list.
    

I hope this will help you to achieve your goal. If you have any question just ask. Good luck

这篇关于在Java中操纵一个文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆