Results 1 to 3 of 3
  1. #1
    sugard is offline Member
    Join Date
    Mar 2009
    Rep Power

    Default getting data from a file


    I have a Client and a Server. Now the Client need to read data from a file and transfer it to the Server. Though the client needs to transfer this data in blocks. This is since when having a large file it is difficult to send it as a whole.

    What I tried to do is setting a number of blocks such as 4 and then get the size of the file and divide the filesize / number of blocks to get the size of each block. Then transfer each block.

    When I am trying to compile the following code (Client class), I am having the following errors:

    run: C:\data.txt (The system cannot find the file specified)
    at Method)
    at<init>(FileInputStream.jav a:106)
    at client.ClientMain.main(
    Couldn't get I/O for the connection to: localhost

    I am sure that the file is located there. I also tried writing C:/data.txt instead of C:\ data.txt though still the same error appears.

    If someone can help me it would be really appreciated

    Java Code:
    public class ClientMain {
       public static void main(String[] args) throws IOException {
            Socket kkSocket = null;
            PrintWriter out = null;
            String host = "localhost";
            File f = new File("C:\\data.txt");
            try {
                FileInputStream fstream = new FileInputStream(f);
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                long fileSize = f.length();
                kkSocket = new Socket(host, 4444);
                out = new PrintWriter(kkSocket.getOutputStream(), true);
                int noOfBlocks = 4;
                double blockSize = (double)fileSize / noOfBlocks;
                double lastBlockSize = (double)fileSize % noOfBlocks;
                out.print(noOfBlocks + "," + blockSize + "," + lastBlockSize + "\n");
                // Read file line by line
                while ((strLine = br.readLine()) != null)   {
                } catch (UnknownHostException e) {
                    System.err.println("Don't know about host: " + host);
                } catch (IOException e) {
                    System.err.println("Couldn't get I/O for the connection to: " + host);

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    C:\\ is not the same thing as localhost - what Java calls a file is actually very much an abstraction of what a file is.

    The definition I like is file: A an ordered sequence of bytes.

    Okay, unless you want to do some fancy shifting, rethink the whole thing, do your file read on fstream and skip the localhost stuff for now.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    Try this

    Java Code:
    File f = new File(new File("c:\\"),"data.txt");
    Also, if you are using something like wordpad, it might be adding an extra extension to your 'data.txt' file. To be sure it realllllly is called 'data.txt', open a command prompt, cd to c:\\ and do a dir to make sure your file is there with the expected name.

Similar Threads

  1. Regarding Tabular data from .doc file
    By praveen83 in forum Advanced Java
    Replies: 10
    Last Post: 03-23-2010, 11:26 AM
  2. data file structure
    By Nicholas Jordan in forum Advanced Java
    Replies: 2
    Last Post: 01-07-2009, 04:16 AM
  3. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  4. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 PM
  5. Reading Data from a file
    By ramachandran in forum New To Java
    Replies: 2
    Last Post: 10-24-2007, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts