0

I have a text file that contains value like this:

name1, value1, number 1,
name2, value2, number 2,

I search in this file with the first value (let say name2) and I want to put in the list value2 and number2. How can I do this? For the first value I did it but I don't know how to do to store all the date from that line..

  public static List<String> findLocalityValues(String path, String word) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(path)).useDelimiter(word + ",");

    List<String> list = new ArrayList<String>();
    while (scanner.hasNext()) {
      String found = scanner.next();
      list.add(found.substring(0, found.indexOf(',')));

    }
    return list;
  }
Mark Pope
  • 11,244
  • 10
  • 49
  • 59
  • Are you saving this file yourself? If so, you can just save the file using serialization and then deserialize the file back into a structure you can search, such as a HashTable (assuming you are storing them as a HashTable to begin with). You can do the same with XStream if you want a human-readable file. If this is an option, I will write up an answer for you. – dberm22 Jan 23 '14 at 13:07
  • Thx but I hope to be a solution for this case without changing the file :( – user3227847 Jan 23 '14 at 13:10
  • *"I don't know how to do to store all the date from that line"* Sorry I don't understand what you're talking about. I suggest you make an example of file and how you want this in your map. – m0skit0 Jan 23 '14 at 13:12
  • I want to store date from the specified line (line that begin with that "word" arg) in the list. – user3227847 Jan 23 '14 at 13:15
  • Possible duplicate question http://stackoverflow.com/questions/5600422/method-to-find-string-inside-of-the-text-file-then-getting-the-following-lines – Mani Jan 23 '14 at 21:40

1 Answers1

0

You are making it more complicated than it needs to be. You should be utilizing built-in java utilities. Try something like this:

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

byte[] contentbytes = Files.readAllBytes(Paths.get("path"));
String filecontents = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(contentbytes)).toString();
filecontents = filecontents.substring(filecontents.indexOf("name"));
filecontents = filecontents.substring(0,StringUtils.ordinalIndexOf(filecontents,",", 3));
List<String> myList = Arrays.asList(filecontents.split("\\s*,\\s*"));
dberm22
  • 3,187
  • 1
  • 31
  • 46
  • What if the text file is bigger ? read all content and keep it in jvm is not necessary! – Mani Jan 23 '14 at 18:10
  • @Mani, that's a good point... this may not be the best for huge files, but I'm not sure what I would do in that case. If all you are worried about is memory, garbage collection should take care of most of that (hopefully). You can manually set some objects to null when you are done with them, but that doesn't help as much as you'd think. If you have good suggestion, feel free to provide an answer that might work better for large files. – dberm22 Jan 23 '14 at 21:13
  • @dberm22 In your solution, Garbage collection would not help because the contentbytes , filecontents , myList would be in stack since it is has strong relationship with other instances. ( actualy all of them contain same information so it is O(3n) . i am not sure what is the purpose of setting myList again because the myList already has valueString and NumberString while you split by space isnt ? Did your myList contain only the matched line ? – Mani Jan 23 '14 at 21:35
  • @Mani, I don't get that response. After the creation of filecontents, contentbytes is no longer used. Hopefully, garbage collection would be smart enough to remove that from memory. You can manually set filecontents to null after you're done using it, but that isn't that doesn't really help so much. Same with filecontents and myList. Yes, they are huge memory users, but after they are done being referenced, garbage collection would remove them from memory (in a perfect world). What are you suggesting, other than manually setting the variables to null and calling System.gc() ? – dberm22 Jan 23 '14 at 21:43
  • valueString and numberString are the new values he wants to overwrite the old ones with. (unless I am misunderstanding the question) – dberm22 Jan 23 '14 at 21:45
  • @Mani, It appears I might have been misinterpreting the question. See if you like my update better. – dberm22 Jan 23 '14 at 22:05
  • @dberm22 this will work if the file contain only one occurrence.. but stil it is not addressing the file size. i am concerned about the file size, assume if the file is 300Mb , while ask JVM to readAllBytes you have lost 300Mb . what if the size is more than that. And back to GC - In general variable with in the block considered as valid in scope and not eligable for GC. though recent JIT compiler can able to detect and mark it as out of scope it is not advisable (http://bugs.sun.com/view_bug.do?bug_id=6721588) – Mani Jan 23 '14 at 22:19
  • I would suggest to write simple BR and loop through line and check if the line is exists or not . in that way the at given time only the line is in scope and he can break if he knew for sure it appears only once. i gave link to him in comment to follow. i dont even recomment to have scanner because scanner.next() would give entire record ( which may cause out of memory) – Mani Jan 23 '14 at 22:22
  • That's a good way to go about it, but for short files, this code will work. It's short and clean, though you're right..it's not the most efficient way. I really don't want to modify my answer yet again, so feel free to write up your own solution. Good points though. – dberm22 Jan 24 '14 at 01:11
  • Thanks, i did already provided another SO link in the comment section. he should able to follow. And yes for short files your solution is clean. – Mani Jan 24 '14 at 14:22