简体   繁体   English

如何在特定文件位置附加和更改数据

[英]How to append and alter data in specific file locations

Good evening Stack Overflow! 晚上好,堆栈溢出! I am working on a project that resembles Windows Access Control Lists, and have run into an issue. 我正在一个类似于Windows访问控制列表的项目,但是遇到了问题。 I need to use files to keep track of who is in what group, so for instance, in my file, I want to have have this: 我需要使用文件来跟踪谁在哪个组中,因此,例如,在我的文件中,我想拥有以下内容:

  • Administrators: admin, mike 管理员:admin,mike
  • Users: admin, mike, sarah, eric 使用者:admin,mike,sarah,eric
  • Accounting: sarah, eric 会计:莎拉(Sarah),埃里克(Eric)

I've been simply appending data onto the list, so it would be more like this: 我一直只是将数据追加到列表中,所以更像是这样:

  • Administrators: admin 管理员:admin
  • Administrators: mike 管理员:mike

I also need to be able to add things out of order, so for instance, if I added Admin to Admins, Sarah to Accounting, then Mike to Admins, is there a way to keep all of the data in order? 我还需要能够无序添加内容,例如,如果我将Admin添加到Admins,将Sarah添加到Accounting,然后将Mike添加到Admins,是否有办法使所有数据保持顺序?

I was wondering how to solve this problem, to append data to specific locations so that it doesn't look like a train wreck. 我想知道如何解决此问题,将数据附加到特定位置,以免看起来像火车残骸。

Thanks! 谢谢!

You cannot append data directly into the file. 您不能将数据直接附加到文件中。 You can read it all and change insert data into specific positions, using RandomAccessFile . 您可以使用RandomAccessFile读取所有内容并将插入数据更改到特定位置。 Here is a nice tutorial on how to do so: http://tutorials.jenkov.com/java-io/randomaccessfile.html 这是有关如何执行此操作的很好的教程: http : //tutorials.jenkov.com/java-io/randomaccessfile.html

Below is how you insert thing in the middle of a file that is loaded in memory: 以下是如何在装入内存的文件中间插入东西:

// open file for reading and writing
RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw");

// go to a certain position
file.seek(200);
// write hello world in ths position
file.write("Hello World".getBytes());

// close file - this should be done in the finally clause
file.close();

However, I would not follow this approach. 但是,我不会遵循这种方法。 Why don't you put everything in CSV files, read them using Java, put the content into a list, do whatever inserts, deletes and sorting you wish (sorting is alredy there for Strings), and then save the files into the the file system again? 您为什么不将所有内容都放入CSV文件中,使用Java读取它们,将内容放入列表中,进行所需的任何插入,删除和排序操作(对字符串进行排序,然后将其保存)到文件中系统再次?

You can use OpenCSV to read CSV files, and below is an example: 您可以使用OpenCSV读取CSV文件,下面是一个示例:

... import java.util.*;

public class ParseCSV {
  public static void main(String[] args) {
    try {
      //csv file containing data
      String strFile = "MyCSvFile.csv";
      CSVReader reader = new CSVReader(new FileReader(strFile));
      String [] nextLine;
      // list holding all elements - they can be later be maniulated
      List<String> elements = new ArrayList<String>();
      while ((nextLine = reader.readNext()) != null) {

        for (int i = 0; i < nextLine.length;i++) {
            elements.add(nextLine[i]);
        }
      }
    }
  }
}

Following this approach, then in order to insert elements in the middle, or in the end of the list is a matter of using the data structure appropriately, and finally writing them to disk, alo using OpenCSV if you wish. 按照这种方法,然后要在列表的中间或列表的末尾插入元素,需要适当地使用数据结构,最后将它们写入磁盘,如果需要的话,也可以使用OpenCSV

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM