I have a bunch of text files with entries that look like this:
20662 = {
name="Durgulel"
dynasty=100603
martial=7
diplomacy=4
intrigue=5
stewardship=4
learning=6
religion="orthodox"
culture="alan"
trait="chaste"
trait="arbitrary"
trait="scholarly_theologian"
father=41592
992.1.1={
birth="992.1.1"
}
1068.1.1={
death="1068.1.1"
}
}
Is there a standard way for reading values from something like this into variables?
You could use a StreamTokenizer; should look roughly like this (untested):
static Map<String,Object> parse(
StreamTokenizer tokenizer, int endToken) throws IOException {
Map<String,Object> result = new TreeMap<String,Object>();
while (tokenizer.nextToken() != endToken) {
String key = tokenizer.sval;
if (tokenizer.nextToken() != '=') {
throw new RuntimeException("'=' expected");
}
if (tokenizer.nextToken() == '{') {
result.put(key, parse(tokenizer, '}'));
} else {
tokenizer.nextToken();
result.put(key, tokenizer.sval);
}
}
return result;
}
static void main(String[] args) throws IOException {
Reader reader = new FileReader(args[0]);
Map<String,Object> parsed = parse(
new StreamTokenizer(reader), StreamTokenizer.TT_EOF);
System.out.println(parsed);
}
p.s. This will need some additional handling for 922.1.1 and similar keys. If the format is really just line based, it might be more straight forward to use BufferedReader.readLine(), trim() and check for { / } / =
p.p.s If by variables you mean instance member variables, you could use the Java reflection API to set them accordingly (instead of reading to a map as in the example). You'd hand in the object to fill and then look up and set the members via object.getClass().getField(key).set(object, value)
Yes, there are a few ways to read files into a program. I'd recommend reading about BufferedReader, FileReader, and Scanner. Some documentation on the following:
FileReader
BufferedReader
Scanner
I usually pair BufferedReader and FileReader because the BuffereReader method nextLine() is conveniant for string variables. It's implemented as follows:
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String x = br.readLine();
I'd also recommend some sort of regex expression for reading in your lines because of how the document is formatted. An example of an way of reading in seperate words into indices of a String[] with readLIne() and a simple regex expression is: String wH[] = br.readLine().split("\\W+"); This might be useful for splitting each line based on its contents, but since your lines don't appear to be separated by spaces, it always viable to just examine the raw string based in by readLine() in some sort of for-loop.
Once you've targeted the exact values you want, you can assign them to variables. For example, lets say you have a condition that detects the phrase "name" in a line, you could then assign the value that comes after the = and between the quotes to your desired variable. Then Rinse and Repeat.
Hope this helps.
Related
I have a text file with the list inside, every line has data that I need insert in the new objects. So data looks like somename=3000 or another type with the slash data another type = 6000.
I have particular class "Item" that has String and int variable. Data need to be inserted into them. Every new object has to be added to the ArrayList<Item>.
// Calculate the lines for next for each loop
int lineCount = 0;
while (sc.hasNextLine()) {
lineCount++;
sc.nextLine();
}
for (int i = 0; i < lineCount; i++) {
// creating the object
Item item = new Item();
// add item object to items ArrayList
items.add(item);
// add line to String variable lineToString,
while (scaner.hasNextLine()) {
String lineToString = scaner.nextLine();
sc.nextLine();
}
So, I figured out that to do this, I need to
copy the whole line and put into some string variable;
split it for integer and string variable;
insert string parts to the String variable and numbers to the int variable in particular object that was created in iteration time of "for loop".
add the object with a data inside to the ArrayList.
I used Scanner to read a text file. When I try to insert the scaner.nextLine to the String it's doesn't work; I mean it's executing but variable String lineToString doesn't have the line from a text file.
Could somebody help with an idea of how better to proceed with this problem? Maybe there is some simpler way to insert the 2 different type of data from the text file line in the object and put it into the ArrayList? Every line in the text file has different data and has to be in different objects.
You didn't mention clearly the line format from the text file. I assume so far you have text file in which each line is like
someone=140000
And you are trying to read those lines of the text and parse each of them to an object of Item which contains a String property (I assume you name it name) and an int property (I assume you name it number)
If this is it, you fisrt need to read your text file line by line and process it further. There are several ways to read a text file line by line.
BufferReader
This is a very common and so far most appropriate way to read a text file in consider of performance.
List<Item> particulatItems = new ArrayList<>();
// using try-with-resource that will help closing BufferedReader by own
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
particularItems.add(processLine(line));
}
}
Scanner
You could use Scanner too.
try (Scanner scanner = new Scanner(new File(fileName))) {
while (scanner.hasNext()) {
particularItems.add(processLine(line));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
You should extract the line processing logic to a independent function. This is good practice of clean coding.
public static Item processLine(Strinng line) {
String[] tokens = line.split("=");
Item item = new Item(tokens[0], tokens[1]);
}
Assuming you have that particular object defined as Item and you are populating a List of this type
public class Item {
String name;
int number;
public Item(String name, String numtxt) {
this.name = name;
this.number = Integer.parseInt(numtxt);
}
// setter getter
}
More reading:
How to read a large text file line by line using Java?
Different ways of Reading a text file in Java
Difference between Scanner vs BufferReader
Try-with-resources in Java 7
Looks like you have already scanned full file in below code snippet:
while (sc.hasNextLine()) {
lineCount++;
sc.nextLine();
}
After this, you are again iterating in for-loop but with same scanner, which has read last line
So the following may return false:
while (scaner.hasNextLine())
I it may never enter while loop
You should reset scanner before iterating lines again .. or may be use something else than scanner to count lines
Apart from what #Ashish Mishra mentioned, you are doing the second while loop in a for loop, why? Isn't one loop sufficient?
int lineCount = 0;
while (sc.hasNextLine()) {
lineCount++;
String lineToString = sc.nextLine();
Item item = new Item();
//convert lineToString to item
items.add(item);
}
I'm currently in a High School level Java course. I've been doing plenty of research here, on Stack Overflow, trying to work through a project i'm currently assigned. The project consists of making modifications to, and searching through, various words pulled from an encyclopedia file. This is what I am having trouble with, the very basic form of this project. I already found the method in which to solve this problem, but i wasn't able to find a good way of implementing it. This is a copy of the code i found here: (the third method down contains the portion i took from this site)
class word
{
public String newString;
EasyReader fileIn = new EasyReader("Encyclopedia.txt");
EasyWriter fileOut = new EasyWriter("writeHere.txt");
String fileName="Encyclopedia.txt";
private String onFile;
public word()
{
onFile="";
}
public word(String s)
{
onFile=s;
}
String file = "Encyclopedia.txt";
private String readFile(String file) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line=null;
StringBuilder stringBuilder = new StringBuilder();
String is=System.getProperty("line.seperator");
while((line=reader.readLine())!=null)
{
stringBuilder.append(line);
stringBuilder.append(is);
}
newString=stringBuilder.toString();
return stringBuilder.toString();
}
}
So, the question: how do i use this method? i know it sounds silly, but how do run this method and then use the data later? It is supposed to take a given text file and return a string, but i'm not even sure how to get the return value after it has processed.
Any help would be greatly appreciated. I made an account here just to ask this question. If i need to post this somewhere else, or if there is a better site to use to find an answer and some more basic help, please let me know. Thanks,
-Ethan
The readFile method seems to be doing multiple things at once. It accepts a file argument which overrides the member variable with the same name. Then it reads the file into a String and sets the newString member variable to the result before returning the same result.
So I would recommend first deciding whether the method should return the data or set the member variable. If multiple methods are going to be using the result, it might be useful to use the member variable, otherwise go the return route. Also, you can probably remove the file member variable since it is ignored by the method.
You can rewrite the method to look like this (I just removed the newString=stringBuilder.toString(); line, and I changed it to static since it can be):
private static String readFile(String file) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line=null;
StringBuilder stringBuilder = new StringBuilder();
String is=System.getProperty("line.seperator");
while((line=reader.readLine())!=null)
{
stringBuilder.append(line);
stringBuilder.append(is);
}
return stringBuilder.toString();
}
And wherever you need to use it call it like this (remember to catch the IOException):
try {
String someString = readFile("filename.txt");
} catch(IOException e) {
// handle error
}
Remember it must be called from inside the same class unless you change private to public.
Also, it might be worth reading and following a standard code style. It can really help by distinguishing between different types of variable for example.
Actual method that read string from file is: "readFile". And in your class, you are not calling that.
private String readFile(String file) throws IOException
You can pass file name as parameter, and It will returns read string.
So, how about modify your word(String s), and add method that will return actual result of read file?
public word(String s)
{
onFile=s;
newString = readfile(onFile);
}
public getNewString(){
return newString;
}
Try this:
String file ="/path/to/file.csv"
word myWord = new word();
o = myWord.getClass().getDeclaredMethod("readFile");
o.setAccessible(true);
Object r = o.invoke(myWord);
//print result
I need help making a parallel array I need to read in from a textfile of strings and create a array of strings that adds each name once and increments repeated strings in an array of ints..... any ideas of how i can fix this code to do that?
Scanner sc=new Scanner(new File("Cat.txt"));
String category=sc.nextLine();
int total=sc.nextInt();
int[]totcat=new int[total];
String[]names=new String[total];
while(sc.hasNextLine())
{
String x=sc.nextLine();
boolean b=false;
int size=0;
for(int i=0;i<names.length;i++)
{
if(x.equals(names[i]))
{
b=true;
totcat[i]++;
}
}
if(!b)
{
names[size]=x;
totcat[p]++;
size;
}
}
The problem is the line if(names[j].equals(null)). This will never evaluate to true, since for that to happen, names[j] would have to be null, and so it would instead throw a NullPointerException. The correct way to write it would be if(names[j] == null).
A more elegant way would be to have another variable to keep track of how many strings you have in your array, so that if you don't find a repeated string (your if(!b) block), you can just add the string at the index indicated by the size variable, instead of having to go through the whole array looking for a null space.
It sounds like you need to read a file with Strings that are delimited by line breaks, and you want to end up with a set of all Strings found in the file as well as a count of the number of times each String occurs in the file.
Rather than trying to build an array-based structure, I'd recommend using a Map. As you read each line in the source file you check the map to see if the String value already exists in the map. If so, you increment it's value (an int), if not, you add the String to the Map.
Something like this:
Map<String, Integer> map = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
if(map.containsKey(line)) {
Integer lineCount = map.get(line);
map.put(line, lineCount++);
} else {
map.put(line, 1);
}
}
} catch(IOException ioe) {
// Handle exception
}
Something to watch our for here is memory limitations. If the file you are reading is very large and/or your Java stack size is small, you may encounter OutOfMemoryExceptions.
I'm stuck.. I trying to parse a text from file in words, but save it in List of objects. Whether it is possible to do so?
public class Text {
public static List<Words> words = new ArrayList<Words>();
}
public class Words {
private String path;
private String[] inside;
private BufferedReader in;
public Words(String path, String[] inside) {
this.inside = inside;
this.path = path;
}
public String[] splittinIntoWords() throws IOException {
in = new BufferedReader(new FileReader(path));
String s;
while ((s = in.readLine()) != null) {
inside = s.split(" ");
//System.out.println(Arrays.toString(inside));
}
return inside;
}
}
and main class
public class Main {
public static void main(String[] args) throws IOException {
String file_name = "book.doc";
String[] inside = null;
Words w = new Words(file_name, inside);
w.splittinIntoWords();
Text.words.add(w); //after add in list i have a reference.
System.out.println(Text.words.toString());
}
}
i do smthg wrong. I understand how to do this with List of Strings
tell me please, it is possible, to add text splitting into words in List of Words
You’re overwriting the array of words Words.inside with each line you read. You need to add the output of split() to a List every time round the while loop, not just at the end.
I would expect your code to display the words in the last line of your file, but possibly it has a blank last line, in which case you will see nothing.
Also, I assume your "book.doc" is not really a .doc format file—word processor files need special parsing; what you have written will only work on plain text files.
there is several things wrong with your code.
Text.words shouldn't be static. Every instance of text consists of a different collection of words.
When you make a "Collection of Words", then it should be Collection< Word>. Because every item inside the collection is just a single item.
but then again, Collection< Word> is just the same as Collection< String>. So use that.
"path", "in" should not be member variables of "Words". Just use them locally in your method. Especially since you never closed "in".
you're overwriting whatever is in "inside" for each line in your file that you loop over. When you have your "Collection< String> words", then just do words.addAll(inside); in
your loop.
Yes, I know this is not an answer, but I'm trying to point you in the right direction. This might help you more in the long run.
How can you make the efficient many-to-many -relation from fileID to Words and from word to fileIDs without database -tools like Postgres in Java?
I have the following classes.
The relation from fileID to words is cheap, but not the reverse, since I need three for -loops for it.
alt text http://img191.imageshack.us/img191/4077/oliorakenne1.png
My solution is not apparently efficient.
Other options may be to create an extra class that have word as an ID with the ArrayList of fileIDs.
Reply to JacobM's answer
The relevant part of MyFile's constructors is:
/**
* Synopsis of data in wordToWordConutInFile.txt:
* fileID|wordID|wordCount
*
* Synopsis of the data in the file wordToWordID.txt:
* word|wordID
**/
/**
* Getting words by getting first wordIDs from wordToWordCountInFile.txt and then words in wordToWordID.txt.
*/
InputStream in2 = new FileInputStream("/home/dev/wordToWordCountInFile.txt");
BufferedReader fi2 = new BufferedReader(new InputStreamReader(in2));
ArrayList<Integer> wordIDs = new ArrayList<Integer>();
String line = null;
while ((line = fi2.readLine()) != null) {
if ((new Integer(line.split("|")[0]) == currentFileID)) {
wordIDs.add(new Integer(line.split("|")[6]));
}
}
in2.close();
// Getting now the words by wordIDs.
InputStream in3 = new FileInputStream("/home/dev/wordToWordID.txt");
BufferedReader fi3 = new BufferedReader(new InputStreamReader(in3));
line = null;
while ((line = fi3.readLine()) != null) {
for (Integer wordID : wordIDs) {
if (wordID == (new Integer(line.split("|")[1]))) {
this.words.add(new Word(new String(line.split("|")[0]), fileID));
break;
}
}
}
in3.close();
this.words.addAll(words);
The constructor of Word is at the paste.
Wouldn't a more efficient approach be to assign the link from Word to MyFile at the point that you know the Word is in the File? That is to say, how do you build the list of Words in the MyFile object? If you're reading the words in to the MyFile out of, say, a file on the filesystem, than as you read in each word, you assign its MyFile to the current file.
//within MyFile constructor or setter for Words
while (//there's another word to add) {
Word newWord = new Word(//read word from file);
words.add(newWord);
newWord.setMyFile(this);
}
This is akin to the typical way to manage a bidirectional parent-child relationship:
//in Parent
public void addChild(Child child) {
myChildren.add(child);
child.setParent(this);
}
It might help if you show us how you build the MyFile object.
Edited after you added the code that builds the list of Words:
OK, so having seen the code that builds your Words, I don't think setting up the relationship is the source of your inefficiencies. It looks like you are setting up the relationship in exactly the way I suggested (as you add each word, you give that word the fileID of the corresponding file).
It looks like the source of your inefficiencies are that, for each word, you have to match it up with various things that you currently have in a set of files (e.g. WordToWordId). So for every word you have to loop through every line of that file, and find the match. This is certainly inefficient.
The better approach is to have those pairings in memory in a HashMap, initialized at startup. That way, if you have a particular word and need the corresponding ID, or vice versa, you look them up in your HashMap, which is a constant-time operation. Similarly, for each word, you are looping through every file; again, do that loop ONCE, and store the result in a HashMap. Then lookups become constant time.
Both classes should override hashCode and equals. Thus you will decide what is equal.
Then you will create a set in each of your classes.
public class MyFile implements Comparable<MyFile> {
//your fields here
Set<Word> words = new HashSet<Word>(0);
//Remember to override hashCode and equals
}
public class Word implements Comparable<Word> {
//your fields here
Set<MyFile> words = new HashSet<MyFile>(0);
//Remember to override hashCode and equals
}
In your sets now you will have all the MyFiles.words and otherway around, all the Words.myFile
I think you want that the file know it's words and the words know the files where it is used.
public class File {
private List<Word> words;
public File(){
words=new Vector<Word>();
}
/**
*The method add word to word list.
**/
public addWord(Word word){
this.words.add(word);
word.addFile(this);
}
}
public class Word{
List<File> files;
public addFile(File file){
this.files.add(file);
}
}
or vice versa... but you should question GRASP Design pattern.Maybe your data type is wrong (I dont say wrong because itis your desing,so i respect).