New I/O and Text files

Hi,
I've to read a text file by chunks of SIZE byte sequentially. Then, for each loaded chunk I should access its content line by line (separated by \n).
The first solution I made is to load the file line by line with a BufferedReader counting the number of characters loaded so far, forming in memory the chunk and using it. But I think that this is too slow and I don't know why but for SIZE value bigger than 100MB I got an OutOfMemoryExceptiom (I'm giving 1GB of RAM to JVM using -Xmx1024m). So actually I'm forced to use a SIZE value of 50MB or 80MB.
I think that I can improve the performance of this chunking process using the New I/O (probably fixing in this way also the OutOfMemory bug). But the problem is how to use that package to read a block of SIZE bytes into memory, convert them in chars and accessing them line by line.
I've tried something like:
ByteBuffer buff = ByteBuffer.allocate(SIZE);
int size = ch.read(buff);
char[] chars = buff.asCharBuffer().array(); // <- HERE THE PROBLEM
if (ch.position() != ch.size()) {
     int k = size - 1;
     char chrt = chars[k];
     while(chrt != '\n' && k >= 0) {
          ch.position(ch.position() - 1);
          chrt = chars[--k];
CharArrayReader chr = new CharArrayReader(chars);
BufferedReader br = new BufferedReader(chr);
But the problem with this code is that the .array() method gives me a UnsupportedOperationException.
As you can see by my code, another problem is the alignment of the last loaded string that can be "cutted" in the middle. So my idea was to walk back from the end of the loaded chunk of chars to the first new line (moving the current channel position). I don't know if it works or there is a better solution (I've the previous problem before!).
Any ideas/hints?
Thanks!

Forget all this and find a way to process the file line by line.

Similar Messages

  • CLIENT_TEXT_IO : cannot insert new line into a text file

    Facts:
    Developer 10g
    AS 10g
    Web utility
    Problem:
    i am traying to
    Open a text file. using CLIENT_TEXT_IO.FOPEN
    insert a record in to a text file using CLIENT_TEXT_IO.PUTF
    insert a new line using chr(10) or CLIENT_TEXT_IO.NEW_LINE
    or CLIENT_TEXT_IO.put_line
    insert the next record
    Close the file.
    all work except insert a new line
    Why !!!
    it was Working fine in 6i, but now in 10g and Webutil is not working
    plz help

    Hi you can put a new line in a text file using System.getProperty("line.separator"). This implementation will work for every OS.
    import java.io.*;
    class Demo{
    public static void main(String args[]) throws Exception {
    FileWriter fr = new FileWriter("FileDemo.txt");
         fr.write("AAAAAAAAAA");
    fr.write(System.getProperty("line.separator"));
    fr.write("AAAAAAAAAAA");
    fr.close();
    }

  • Saving report as PDF and text file

    Currently, I saved my report as PDF. It opens in Acrobat reader automatically when the report is executed. Is there a way for me to save it as PDF and as a text file at the same time? (without having to open the file in Acrobat reader and saving it)

    You can add your PDF and TEXT file formats to a distribution list (Instead of GENERATE REPORT TO A FILE). To do this:
    1. Go to your Object Navigator and highlight your report.
    2. Right click on your report. Select Property Palette.
    3. Select Distribution
    4. You can add of of the formats that you want to print or save to the report to.
    5. You will have to add Distribution Id (any number starting with 1) where you want the file to be saved (c:\my_reports\inventory.pdf), the type of file: PDF and how many copies you want: 2
    6. Click OK after finishing...
    To make it work ... You would highlight your report from your Object Navigator and then choose FILE-DISTRIBUTE and this will save it to all the files your specified and/or printer.
    null

  • How to insert new line in a text file

    hi all,
    i wnat to know how can i insert a new line in a text file using java.
    for example i want the formate of the text like this
    1 2 3
    4 5 6
    until now i know only how to insert data but not new line.
    Thanks in advandce

    Hi you can put a new line in a text file using System.getProperty("line.separator"). This implementation will work for every OS.
    import java.io.*;
    class Demo{
    public static void main(String args[]) throws Exception {
    FileWriter fr = new FileWriter("FileDemo.txt");
         fr.write("AAAAAAAAAA");
    fr.write(System.getProperty("line.separator"));
    fr.write("AAAAAAAAAAA");
    fr.close();
    }

  • What are some of the best iOS apps can remotely played videos, audios, photos and text files from a NAS hdd connected to Airport Extreme USB port? And how to configure this setup?

    I have already set up NAS hdd as connecting it at USB port of Airport Extreme, i also want to remotely access it from iPhone, so what's the next step? What are some of the best iOS apps can remotely played videos, audios, photos and text files from the NAS hdd and how to configure this setup?

    *Edit - I am not able to connect to the NAS when hardwired to the airport extreme.

  • Aloha, I have a CD-Rom from 2001 that i very much want to access but cannot because i no longer have "classic." There are a variety of video and text files. What is the best way to convert? Any help is greatly appreciated. mahalo.

    Aloha, I have a CD-Rom from 2001 that i very much want to access but cannot because i no longer have "classic." There are a variety of video and text files. What is the best way to convert? Any help is greatly appreciated. mahalo.

    Buy a cheap older Mac like an iMac.
     Cheers, Tom

  • Want to know how to check for new line character in text file

    Hi All,
    I`m trying to read data from text file. However I`m not sure whether the data is in 1st line or nth line. Now I`m trying to read the text from the readline. But if text is "" and not NULL then my code fails. So I want to know how to check for new line character and go to next line to find the data. Please help.
    Thanks
    static int readandwriteFile(Logger logger,String filepath){
              BufferedWriter out = null;
              BufferedReader in = null;
              File fr = null;
              int get_count = 0;
              try     {
              if(new File(filepath).exists())
              fr= new File(filepath);
                        System.out.println("FileName: "+fr);
                   if(fr != null){
    in = new BufferedReader(new FileReader(fr));
                             String text = in.readLine();
                             if(text != null){
                             get_count = Integer.parseInt(text);
                             in.close();
                             else{
                                  get_count = 0;
         else{                    
    out = new BufferedWriter(new FileWriter(filepath));
         out.write("0");
                out.close();
                   }          //Reading of the row count file ended.
              catch(Exception e) {
                   e.printStackTrace();
              finally {
                   try{               if (in != null) {
                             in.close();
              if (out != null) {
                             out.close();
              catch(Exception e) {
                        e.printStackTrace();
              return get_count;
         }

    You are calling the readline() only once which means you are reading only the first line from the file...
    Use a loop (Do-While preferably)
    do{
    //your code
    }while(text == "")

  • Inserting new line to a text file at position #

    Hi,.
    Is it possible to do this in java?
    1. Open a new text File (woutFile)
    2. woutFile.write(" line #1\r\n"); // write new line
    3. woutFile.write(" line #2\r\n"); // write new line
    4.woutFile.write(" line #3\r\n"); // write new line
    5. Go to line #2 position on a text file
    6. Insert NEW LINE between line #2 and Line #3
    7. move to the end-of-file
    8.Close the file
    Please help

    Is it possible to do this? please look at question
    inside comments.Your question has already been answered. Beyond that,
    if you have some code and you want to know if it will
    work, then run it and see.The answer was no. It's one of the fundamental properties of files that you cannot insert content in the middle, just at the end. To insert content you first have to make room for it. In this respect a file works like an array and not like a linked list.

  • How to create Base Dimensions with MaxL and Text File?

    Hi,
    Doing a scratch rebuild of a cube every month. Don't want to have a 'dummy' outline with base dimensions to copy over every build. Instead want to build from text file somehow. Thus my plan is to: 1) Delete the existing app/db 2) Create a new blank app/db 3) Create the base dimensions in the outline via text file and 4) Build entire outline via a text file. I'm stuck on #3 how to get the 'base dimensions' built via text file. I need:
    ACCOUNTS
    PERIOD
    VALUE
    VIEWS
    SCENARIO
    CUSTOM4
    YEAR
    CUSTOM3
    CUSTOM2
    ENTITY
    CUSTOM1
    I see this MaxL, but it uses a 'rules file' and I never have built a rules file to create base dims so I'm confused if it's possible or not...
    import database sample.basic dimensions
    from data_file '/data/calcdat.txt'
    using rules_file '/data/rulesfile.rul'
    on error append to '/logs/dimbuild.log';

    We rebuild our Departments and Organization from an enterprise hierarchy master each week.
    The way we implemented (what you call #3) was to not do #1 and #2, but to have a "destructive" load rule for each of these dimensions using a text file. (in the "Dimension Build Settings" for the load rule, select "Remove Unspecified" to make it destructive)
    The text file just has the dimension name (parent) and any children we needed defined in a parent/child relationship. For instance
    "Sales Departments" "0100-All Departments"
    This essentially works the same as deleting the app because the destructive load rules will drop all the blocks of data that were unspecified.
    Then we run our SQL load rule to build the rest of the dimensions from the Location Master.
    We perform a level-0 export prior to this process, then reload the level-0 data and execute all the consolidation scripts to get the data back (now in a current enterprise defined hierarchy)

  • Working with forms, buttons and text files

    please am new to java am working on the project. please see sample below, can anyone tell me how i can get the button in form1 to open form2 and also how the information entered in the textfield can be written to a text file. THANKS
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Form1 extends JFrame
         static JFrame frame;
         static JPanel panel;
    JLabel label;
         JTextField text;
         JButton btn;
         GridBagLayout g;
    GridBagConstraints gbc;
         public Form1()
              label = new JLabel ("Label");
              gbc.gridx = 0;
    gbc.gridy = 1;
    g.setConstraints(label, gbc);
    panel.add(label);
              text = new JTextField();
    gbc.gridx= 1;
    gbc.gridy = 1;
    g.setConstraints(text, gbc);
    panel.add(text);
              btn = new JButton ("CONTINUE");
              gbc.gridx = 0;
              gbc.gridy = 2;
              g.setConstraints(btn, gbc);
              panel.add(btn);
    import java.awt.event.*;
    public class Form2 extends JFrame
         static JFrame frame;
         static JPanel panel;
    JLabel label;
         JTextField text;
         JButton btn;
         GridBagLayout g;
    GridBagConstraints gbc;
         public Form2()
              label = new JLabel ("Label2");
              gbc.gridx = 0;
    gbc.gridy = 1;
    g.setConstraints(label2, gbc);
    panel.add(label2);
              text2 = new JTextField();
    gbc.gridx= 1;
    gbc.gridy = 1;
    g.setConstraints(text2, gbc);
    panel.add(text2);
    }

    please am new to java am working on the project.
    please see sample below, can anyone tell me how i can
    get the button in form1 to open form2 For opening form2 from form1 you can add a actionListener to the button.When the button is pressed instantiate the other class and render it visible which i leave it to you to implement.
    and also how
    the information entered in the textfield can be
    written to a text file. THANKSUse getText ( ) method of JTextField to retrieve the text from it and then it's ordinary file io operations for which i guide you to the tutorials here
    http://java.sun.com/docs/books/tutorial/essential/io/index.html

  • CF9 CPU at 100%. Moved to new CF11 server and copied files over and it's still at 100% CPU almost immediately on new server

    Our web server is stuck at 100% CPU.  Three days ago we were on CF9 and started running into this problem until the server crashed and wasn't salvageable.  Luckily we had a fresh Windows 2008 Server R2 ready to go with a fresh copy of CF11 on it.  I copied all the website files over and we were back up and running.  A day later the CPU is back to 100% on the new CF11 Windows 2008R2 server.  I also updated CF11 to the latest update 3 that was just released.
    If I turn off the CF service the CPU usage goes back to normal.  If I turn CF back on, the CPU goes back to 100% within like 5 seconds.  So it doesn't seem like some slow running page or anything that eventually eats up all the memory or whatever.  I'm not an expert at looking at the logs, but I don't see anything too out of the ordinary.  The one thing that looks strange to me is I see this line over and over.
    Dec 12, 2014 11:28:41 AM Information [ajp-bio-8014-exec-59] - Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=194', method='get'}
    That line makes me think we got hacked somehow, but it's hard to know.
    I did up my JVM heap size and max size to 1024MB.  We have 4GB of memory.  And i changed teh JVM arguments to what's below based on a forum post.  Nothing seems to help.
    -server -XX:PermSize=192m -XX:MaxPermSize=192m -XX:+UseParallelGC -Xbatch -Dcoldfusion.home={application.home} -Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.JavaUtilLog -Duser.language=en -Dcoldfusion.rootDir={application.home} -Dcoldfusion.libPath={application.home}/lib -Dorg.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER=true -Dcoldfusion.jsafe.defaultalgo=FIPS186Random
    Any additional ideas on how to debug this or what to look at?  Thanks!

    Thanks Charlie for your insight.  I searched everywhere to find a cfhttp request that was causing the issue, but couldn't find it.  I did find some unusual files that were loaded though that I know we didn't put there.  I did a search and found this link that shows the code.  Coldfusion CFIDE bitcoin mining exploit – PHP involved… | code-complete.com and that you mention in your article too.  I don't see any of the executables running or files that were mentioned though.  I found that code in 8 different spots though and removed them.  Maybe our old server had the executables and hacked files on them.  Hard to know as it won't boot up anymore!
    I think I did possibly isolate the HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=218', method='get'} in the http.log file to a certain site in IIS though.  And it wasn't our main site.  I turn off this website in IIS and it appears the weird request goes away.  I turn the site back on and it starts to re-appear.  It doesn't always load constantly so it's a little hard to tell.  This site is pretty small.  I went through each file on the site and did find one file that did appear to be hacked.  It wasn't coldfusion code though.  Just some html links.  I removed it.  That's all I could find.  No cfhttp calls or anything else.
    We re-installed Coldfusion 11 on Friday as well and upgraded to Update 3.  It doesn't stay locked at 100% as much right now, but it being over the weekend we don't get much traffic.  Monday will be the real test.  I think I will leave that smaller site turned off for now and see how things perform.  I'm doing a full virus scan for the heck of it overnight too.  Don't really expect it to find anything though. I also turned on advanced logging in IIS 7.5 and don't see anything out of the ordinary.  I made sure client variables weren't in the registry either.
    Here's a part of the http.log file when I turn the IIS site on.  I turn the site off and it stops popping up in the logs.
    "Information","ajp-bio-8014-exec-57","12/13/14","22:33:14",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=233', method='get'}"
    "Information","ajp-bio-8014-exec-57","12/13/14","22:36:17",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=601', method='get'}"
    "Information","ajp-bio-8014-exec-57","12/13/14","22:38:31",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=459', method='get'}"
    "Information","ajp-bio-8014-exec-57","12/13/14","22:38:54",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=108', method='get'}"
    "Information","ajp-bio-8014-exec-57","12/13/14","22:39:55",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=218', method='get'}"
    "Information","ajp-bio-8014-exec-63","12/13/14","22:52:03",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=54', method='get'}"
    "Information","ajp-bio-8014-exec-64","12/13/14","22:57:32",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=40', method='get'}"
    "Information","ajp-bio-8014-exec-64","12/13/14","22:58:37",,"Starting HTTP request {URL='http://zzen1wbudopwg.nchyt.com/encfm/en0024-ssj5iway6wvg/cbeim94a1s2kebu.php?do=702', method='get'}"

  • Writing to a new line in a text files

    I have a servlet that collects results from a questionnaire by getting the parameter values of the radio buttons on a questionnaire page. I am trying to store the results of the questionnaire by writing them to a text file. I can get it to write to the files fine but cannot seem to get it to write the next set of results to a new line. I have many different ways but cannot seem to get it to work. Any ideas?
    Heres the part of the code that writes to a text file
    String lineRead = "";
                int lineCount = 0;
                while((lineRead = input.readLine()) != null)
                    lineCount ++;
                    String result = request.getParameter("question" + lineCount);
                    output.write(result + ",");       
                    output.write("\n");

    Heres the part of the code that writes to a text
    file
    String lineRead = "";
    int lineCount = 0;
    while((lineRead = input.readLine()) !=
    adLine()) != null)
    lineCount ++;
    String result =
    String result = request.getParameter("question" +
    lineCount);
    output.write(result + ",");       
    output.write("\n");
    Can you do output.writeln(result + ",");?
    Failing that, putting output.write("\n") inside the while loop should do the trick.

  • Generating Postscript file using (psl132.prt) and Text file in one go

    Actually my requirement is to have soft copy , postscript files in a single shot for character mode report because the report fetches data from the table then deletes the data.
    - I am able to generate softcopy, and postscript files same using distribution file for Bitmap report as shown below :-
    [RWRUN REPORT=C:\Report\R_TEST.RDF userid = test / test @ test DISTRIBUTE=YES DESTINATION=C:\Report\bRep.dst   ERRFILE=C:\Report\Final\report.err
    The brep.dst file for bitmap report is as follows:-
    DEST1: DESNAME=C:\Report\tpdf.pdf DESTYPE=File DESFORMAT=PDF COPIES=1 LEVEL=Report
    DEST2: DESNAME=C:\Report\test.prn DESTYPE=File DESFORMAT=BITMAP COPIES=1 LEVEL=Report
    But in case of Char mode report i am able to generate text file but i am unable to generate postscript file for using printer definition file. The commands used are as follows :-
    RWRUN REPORT=C:\Report\R_TEST.RDF userid = test / test  @ test DISTRIBUTE=YES MODE=CHARACTER   DESTINATION=C:\Reports\cRep.dst ERRFILE=C:\Report\Final\report.err
    The crep.dst file for char report is as follows :-
    DEST1: DESNAME=C:\Report\tpdf.text  DESTYPE=File DESFORMAT=ASCII   COPIES=1 LEVEL=Report
    DEST2: DESNAME=C:\Report\test.prn  DESTYPE=File DESFORMAT=prt132.prn COPIES=1 LEVEL=Report  (This option does not work is there any work around to make it work) ]
    I am getting an error. it there any work around for this or there is any other solution.
    Is there any other way i convert my text file to postscript file or viceversa...thanks

    Hi,
    Peter Gjelstrup wrote:
    Next thing is how to use it:
    declare
    k_file constant varchar2(30) := 'YourFile.xls'
    begin
    create_file(k_file);
    -- Call other procedures
    close_file;
    end;
    Don't forget: these procedures are all part of the gen_xl_xml package.
    To call them from outside the package, you have to prefix each procedure name with the package name, like this:
    declare
    k_file constant varchar2(30) := 'YourFile.xls'
    begin
    gen_xl_xml.create_file(k_file);
    -- Call other procedures
    gen_xl_xml.close_file;
    end;

  • Loading from a Flat file: Binary and Text File

    Hi,
    Does anyone know what the difference is between loading from a binary file or text file?
    Ramon

    Hi,
    the difference is that text files contain lines (or records) of text and each of these has an end-of-line marker automatically appended to the end of it whenever you indicate that you have reached the end of a line.
    So what happens when we read from a text file is that the end-of-line character for the operating system that we are using gets converted into a low value end-of-string indicator and when we write to a file the appropriate end-of-line character(s) get written when we indicate the end of the line. This makes the reading and writing of text files much easier because the appropriate end-of-line markers are handled for us.
    With a binary file none of these conversions take place. When we read a binary file the end-of-line characters for our operating system will be read into the string and treated no different than any other character. When we write to a binary file the only end-of-line markers that are written will be those that we code into the output ourselves and hence will be exactly as we code it regardless of the operating system that we are running on. This makes it much easier for us when the file does not contain straight text and the end-of-line marker does not separate lines of text but rather appears as part of the non-text data content of the file.

  • I have a new MAC Pro and transferred files from a PC. Now I have about 100GB of real information but 300GB of other which I can't ID from summing up each folder and sum of applications, music, pictures etc. What could be occuping the 300GB OF OTHER??

    I have a new MacBook Pro and used the tool to transfer information from my HP Computer to the MAC during set up. THe size of the disk space used on the HP was about 200GB. Now I have 400GB used on the MAC. I have added up all the folders and applications etc. and they total only about 110GB. On the summary the "other " category shows over 300 GB. What could it be. I would like to recover the space.

    Check out :  The Storage Display
    http://support.apple.com/kb/HT1408 How to transfer data from a PC to a Macintosh 
    ===============
    Please read the warranty paperwork that came w/your computer.
    You have 14 days to return the computer w/no questions asked.  Plus you have 90 days of FREE phone tech support on top of your standard 1 year warranty unless you also purchased AppleCare which gives you an additional 2 years of coverage plus FREE phone support.
    Strongly suggest that you take FULL advantage of the above before it runs out.  Let Apple deal w/the problems.

Maybe you are looking for