Can somebody help me in reading text from a file without using Java Class Library???

Can you elaborate on that ?

Actually I have to make a hashtable. My data is in a datafile and I have to retrieve data from that without using Java Class Library, that means I cant use import java.io...
I have to take first two words to be the key and then the next word to be the value. Right now I am stuck in reading the data from file.

Thanks for your help.

Actually I have to make a hashtable. My data is in a datafile and I have to retrieve data from that without using Java Class Library, that means I cant use import java.io...
I have to take first two words to be the key and then the next word to be the value. Right now I am stuck in reading the data from file.

Thanks for your help.

Can you post the exact description of your assignment, because I find it strange to read a file without using the java.io ?

I think that condition of not using JCL is only for hashtable or ArrayList, as our professor wants us to make that. So I guess I have to using JCL for reading files as there is not other way out!!
Thanks.

Hey, need a little help, I am using the following code for readin data fom file:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class QuickFileRead {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner =
new Scanner(new File("c:/temp/text.txt")).useDelimiter("\\Z");
String contents = scanner.next();
System.out.println(contents);
scanner.close();
}
}

How can I change this to read data line by line or in a way that I can read words so that I can seperate the keys and their values.
My first two words will be the keys and the next work will be the value.
Thanks.

Don't use throws Exception at main!!!! If it is thrown then your program will crash. You need to catch it:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class QuickFileRead {
public static void main(String[] args) {


Scanner scanner = null;

try {
    scanner = new Scanner(new File("c:/temp/text.txt"));

    String line = null;

     while (scanner.hasNextLine()) {

         line = scanner.nextLine();  
         System.out.println("Line: " + line);

     }
} catch (FileNotFoundException fe) {
   System.out.println("Error: " + fe.getMessage());
} finally {
   scanner.close();
}
}
}

After you get each line, you can use it in any way you want. What is the format of your file. Can you post part of it?

Thanks for the help.

My file has the following data:

Marley was dead; to begin with.
Marley was as dead as a nail-door.
Marley was as dead as a nail-door.
Marley was dead.

And I have to take the prefix store it as key and its suffix as its value in hashtable, for example:

Key -Value
Marley was -dead;
was dead; -to
dead; to - begin
to begin -with
begin with -THE_END
Marley was -dead
and so on................. "-" is only for explaning the difference between key and value
and as we have multiple "Marley was" as key so its value will be a linked list.

Okay, I have written the following code for reading data from file and seperating the keys and value. Its working but plz do let me know if you think something is not good.

public void load_data(String fname) //throws IOException
{
    int counter;
    try
    {
    File file;
    file=new File(fname);

    if(!file.exists()&& file.length()<0)
        System.out.println("The specified file is not exist");

    else
    {
        FileInputStream finput=new FileInputStream(file);
        byte b;
        counter = 0;
        do
        {
            b=(byte)finput.read();             
            if(b == ' ' || b=='\r' || b =='\n' || b == -1)
            {
                counter += 1;
                if(counter >= 3)
                {
                    key =key_part1 + " " + key_part2;                       
                     Hash_key(key);
                     key = "";

                    key_part1 = key_part2;
                    key_part2 = value;
                    if(b == '\n')
                    {
                        value = "THE_END";
                    }
                    if(value == "THE_END")
                    {
                        counter = 0;
                        key_part1 = "";
                        key_part2 = "";
                    }                        
                    //Hash_value(value);
                    value = "";
                    if(b == -1)
                    {                         
                        key = key_part1+ " " + key_part2;
                        Hash_key(key);

                        value = "THE_END";                          
                        //Hash_value(value);
                    }
                }
            }
            else if(counter >= 2)
            {
                value += (char)b;
            }
            else if(counter == 0)
            {
                key_part1 += (char)b;
            }
            else if(counter == 1)
            {
                key_part2 += (char)b;
            }
            System.out.print((char)b);
        }
        while(b!=-1);
        finput.close();
    }

}
catch (IOException ex)
{
   System.err.println("Error: Loading File.");
   System.exit(1);
}
}

Here what I am doing is, in the while loop I keep track of the space in between the words and according to that I seperate my key and value. As my key has two words, thats why I have key_part1 and key_part2.
Once I get my first key and value then I keep swapping words for my key and get the word from file for value. But for next file I start over.
I hope this explanation helps. I know its a long messy program.

Appreciate all your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.