Quantcast
Viewing latest article 18
Browse Latest Browse All 104

java reading a line of specific length and position from a txt fie

I am trying to create segments of data from a text file to transfer over a network to another device however my reader is not working.

It will read the chunk, send it and update the counter value remainigBytes then read the next segment from where the next segment should start. For example HelloWorld would be 2 5 Byte segments of "Hello" and "World" but when i read the file it only reads the first 5 letters("Hello") 2 times and does not read "World" at all.

How do I get the fileReader to read from a specific start point rather than always from the beginning. I am not limited by using FileReader if there is a more appropriate reader I could implement and please let me know.

public int readData() throws IOException {    //creates the file reader for the input file.    FileReader myReader = null;    try {        myReader = new FileReader(inputFileName);    } catch (IOException e){        System.out.println("File reader could not be initialized");        System.out.println(e.getMessage());        System.exit(0);    }    System.out.println("Creating segment...");    System.out.println("----------------------------------------------------");    //reads the input file and changes the segments values    if (this.remainingBytes < this.maxPayload) {        //if the payload size is greater than the number of characters left        char[] segCharsBuf = new char[(int) this.remainingBytes];        myReader.read(segCharsBuf,0,(int)this.remainingBytes);        this.dataSeg.setSize((int)remainingBytes);        this.dataSeg.setPayLoad(Arrays.toString(segCharsBuf));        this.dataSeg.setSq(((int)this.fileSize - (int)this.remainingBytes) / this.maxPayload);        System.out.println("Segment number " + this.dataSeg.getSq() +" created (" + this.dataSeg.getSize() +" Bytes)");        System.out.println("----------------------------------------------------");        return  -1;    } else {        //if the file is longer or just as ling as the payload size        char[] segCharsBuf = new char[this.maxPayload];        myReader.read(segCharsBuf,0, this.maxPayload);        this.dataSeg.setSize(this.maxPayload);        this.dataSeg.setPayLoad(Arrays.toString(segCharsBuf));        this.dataSeg.setSq(((int)this.fileSize - (int)this.remainingBytes) / this.maxPayload);        System.out.println("Segment number " + this.dataSeg.getSq() +" created (" + this.dataSeg.getSize() +" Bytes)");        System.out.println("----------------------------------------------------");        return 0;    }}

Viewing latest article 18
Browse Latest Browse All 104

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>