Reading and writing files in Mac OS X

Hello, I am "kind of" experienced with Java (read a few books, and took one course in highschool, and I fool around with it from time to time).
All of this however was on Windows XP, and all from the command line. Because I now have a Macbook Pro, I wish to start programming java on Mac OSX naturally. I've gotten over the shock and awe of using Xcode (I had never use a "package" before with multiple components), but I have come across a problem. I referenced one of my Java books on how to read from a .txt file (my intended program needs a database), but I've been trying for a few hours now, and I just can't seem to get it to work at all.
I want to read the entire file and save it as a String. I don't know if there is a different procedure on OSX than Windows, but here is what I am using:
import java.io.*;etc. etc.
public String readPlease() {
     String str = "";
     try {
          FileReader file = new FileReader("database.txt");
          BufferedReader buff = new BufferedReader(file);
          str = buff.readLine();
          buff.close();
          return str;
     } catch (IOException e) {
          return e.toString();
}And it is returning:
java.io.FileNotFoundException: database.txt (No such file or directory)The .java file and the .txt file are in the same folder (src) and I've tried it while its in "resources" and "bin" (where the .class files are).

That file needs to be in the directory that you launch your program from.
Is that where it is? It might help if you showed us the command you are using to execute and where you are relative to your file etc.
Also this
BufferedReader buff = new BufferedReader(file);
str = buff.readLine();
buff.close();is going to only read one line of the file. Do you know that?
If you want to read multiple lines you'll need a loop, and you should be using a StringBuilder and appending to it to build your String. Then you can call toString on the StringBuilder instance once at the end to get your string.

Similar Messages

  • Quickest method for reading and writing files

    Hi
    I need help regarding file operations.(Reading and Writing). Currently I am using BufferedReader and BufferedWriter to read and write files. But the files (XML) are very huge files(from 30 -50 mb). This is slowing the application to a great extent. Is there any other approach to perform the above mentioned operations on XML files in a fast manner.
    Thank You
    Mansoor.

    Hi
    Can u let me know how to use the java.nio pavkage for primitve data types(int,float..., boolean). I have tried it but found no success.
    Thank You
    Mansoor

  • Reading and Writing files to external controller with Agilent 4395A

    Hi everyone,
    I have been trying to store the Agilent 4395A Analyzer Compensation Files in an external controller, and then writing them back into the 4395A while running a program. I'm doing this because the 4395A's internal memory and disk storage are not large enough to store all the files I need. I know that the files are binary, and that they can only be read 16kB at a time (the maximum dump by the analyzer). I also know that each dump comes with a specific header that describes how long the dump is.
    My problem is that when I read the files, I believe that something is being corrupted - either by the internal workings of LabVIEW or something in my program. In particular, I notice that when I attempt to use the "Write to Binary File" vi something is being added to the file that I had not intended. At the beginning of the file I notice the string of characters "  @" being added. I know that the analyzer is not giving me these characters so I suspect that LabVIEW is adding them somehow.
    If anyone has any idea about where these extra characters may be coming from, or if you have previous experience with the read/write with the 4395A, any suggestions would be appreciated.
    I have attached an image of the program which saves the files. It is pretty simple, and the only spot where data is actually being read is in the while loop.
    Thank you!
    Matt
    Attachments:
    4395A_SaveFile.jpg ‏2287 KB

    Changing the extension of a bitmap file to get around the prohibition of posting them, is really inappropriate behavior. Use paint to save them as either jpg or png.
    Your problem is probably because the default behavior of the Write to binary File is to prepend array or string size. This is an input to the function and is explained in the on-line help. Turn on Context help and click on 'Details'.

  • Reading And Writing file

    Hi,
    i want to insert some lines of text some where in the text file
    can do it using only one file?
    Plz Help Me

    No.
    Yes, but I'll be charging you $85 per hour for the consulting work.
    This is a horrendously simple thing to do. Have you done a web search for code examples?
    Can you spell the wordl "please" if I gave you $10? How about the word "you"? Do you know what a questino mark is?
    Man...I'm getting sick of these people!

  • Problem on reading and writing from from a *.txt file

    I get Problem on reading and writing from from a *.txt file. The following is the read() method...
    The software said the DataInputStream is depreciated. Can anyone help me please?
    public void read()
        File file = new File("C://Documents and Settings//Charles//My Documents//Brunel//EE2065//Assignment and Lab//Assignment 4 and Lab 4//data.txt");
        FileInputStream in = null;
        String str = "";
        try
          in = new BufferedReader(file);
          //in = new FileInputStream(file);
          for(;;)
            str = new BufferedReader(in).readLine();
            //str = new DataInputStream(in).readLine();
            if(str == null)
              break;
            System.out.print(str);
        in.close();
        catch(IOException e)
            System.err.println("execution error: " +e);
      }

    Thank you for your reply. I have made some change. However, there is an incompetable type found error.
    in = new BufferedReader(new InputStreamReader(in));The following are all of the code.
    public void read()
        File file = new File("C://Documents and Settings//Charles//My Documents//Brunel//EE2065//Assignment and Lab//Assignment 4 and Lab 4//data.txt");
        FileInputStream in = null;
        //BufferedReader in = null;
        String str = "";
        try
          in = new BufferedReader(new InputStreamReader(in));
          //in = new FileInputStream(file);
          for(;;)
            BufferedReader Bstr = new BufferedReader(new InputStreamReader(in));
            //str = new BufferedReader(in).readLine();
            //str = new DataInputStream(in).readLine();
            if(str == null)
              break;
            System.out.print(str);
        in.close();
        catch(IOException e)
            System.err.println("execution error: " +e);

  • File importer detected an inconsistency in the file stucture of (file name). Reading and writing this file's metadata (XMP) has been disabled.

    I have duplicated a project to work on another computer. The project opens fine but when I import new footage/audio files I get this message. "File importer detected an inconsistency in the file stucture of (file name). Reading and writing this file's metadata (XMP) has been disabled." Then I can't play my timeline and Premier Pro crashes.  I have to force quit and restart my computer to continue working. What does this error really mean? How do you rectify?
    Our workflow requires that we duplicate projects to make updates because we are frequently revising but need to keep original project unchanged and intact.

    I have a similar issue and message , but occurs when I import AVI clips from OnLocation CS4 to Premier Pro CS4.

  • IPhone4 how through the USB mobile phone file reading and writing?

    iPhone4 how through the USB mobile phone file reading and writing?

    No idea what you are asking.
    Please explain

  • Reading and Writing large Excel file using JExcel API

    hi,
    I am using JExcelAPI for reading and writing excel file. My problem is when I read file with 10000 records and 95 columns (file size about 14MB), I got out of memory error and application is crashed. Can anyone tell me is there any way that I can read large file using JExcelAPI throug streams or in any other way. Jakarta POI is also showing this behaviour.
    Thanks and advance

    Sorry when out of memory error is occurred no stack trace is printed as application is crashed. But I will quote some lines taken from JProfiler where this problem is occurred:
              reader = new FileInputStream(new File(filePath));
              workbook = Workbook.getWorkbook(reader);
              *sheeet = workbook.getSheet(0);* // here out of memory error is occured
               JProfiler tree:
    jxl.Workbook.getWorkBook
          jxl.read.biff.File 
                 jxl.read.biff.CompoundFile.getStream
                       jxl.read.biff.CompoundFile.getBigBlockStream Thanks

  • Very slow painting while reading and writing doubles into file

    for 15MB length file i = 7662080
    for 50MB length file i = 12414368
    Part of Code for writing into file follows like this:
    try{
    fos = new FileOutputStream("Angel.txt");
    File f = new File("Angel.txt");
         if(f.length() >=4)
         f.delete();
    fos = new FileOutputStream("Angel.txt");     
    dos = new DataOutputStream(new BufferedOutputStream(fos,1000000));
    int x=0;
    double y_last, y_new;
    for(int j=0 ;j<i ;j++)
    if(some condition)
    y_new = ....;
    try{
    //previously in vectors
    y_last = y_new;
    vect.add(new Line2D.Double(x, y_last, x, y_new)_;
    dos.writeDouble(y_new);
         }catch(Exception e){System.out.println(e);}
    dos.close();
    fos.close();
    x++;
    }catch(Exception excp){System.out.println(excp);}
    part of code for reading from file follows like this:
    public void paint(Graphics g)
    try{
         double y1, y2 =0;               
         Line2D.Double doub;
         raf = new RandomAccessFile("Angel.txt","r");
         dis = new DataInputStream(new BufferedInputStream(new FileInputStream(raf.getFD(),1000000)));
         raf.seek((rect.x*8));
         for (int i = 0/any value; (i < value as per choice); i++)
              g2.setStroke(new BasicStroke(0)); //2
              y1 = y2;
         y2 =dis.readDouble();
              doub=new Line2D.Double(i,y1,i,y2);
              g2.draw(doub);
    dis.close();
         raf.close();
    }catch(Exception excp){System.out.println(excp);}
    I tried using Object Streams but NotSerializable Exception is thrown as Line2D.Double objects
    are not serialized.
    Any idea to make reading and writing into file specially from MB files faster is appreciated.

    Why are you reading in the file in the paint method ?
    Create your data once before painting.
    I think you should explain what is your goal and what behavior you want.
    Denis

  • What do I need to do to be able to read and write files to a Seagate Backup Plus Slim Portable Drive on BOTH a Mac and a PC? I downloaded "Paragon NTFS for Mac" onto my Mac, so now I can write to the drive, but when I plug it into a PC it won't work.

    What do I need to do to be able to read and write files to a Seagate Backup Plus Slim Portable Drive on BOTH a Mac and a PC? I downloaded "Paragon NTFS for Mac" onto my Mac, so now I can write to the drive, but when I plug it into a PC it won't work.

    If the HDD is formatted NTFS, then it suggests that there is a problem with the PC.  Check with the manufacturer.
    Ciao.

  • How to read and write files using flex?

    Using flex, we would like to make some utility that will do a
    lot of reading and writing from local files.
    The writing is what I am more worried about. How is this
    possible?
    I want to make .zip files containing many xml files, and some
    assorted media files such as .swf jpeg, html, etc.
    I am new to flex btw, I've never used it properly before, and
    this is the main concern I have with flex, which is that it doesn't
    let me write and read lots of files.
    Mainly we are looking for a good cross platform solution that
    will let us develop an app, and flex looks professional and it is
    cross platform. The "internet app" side of things, isn't so much
    what we need really.
    Would wxWidgets be better suited to our needs?

    The Flash player sand box will not allow reading and writing
    local files. If you want to use Flex to create a desktop you will
    need the Apollo runtime. The alpha is available at Adobe
    Labs.

  • Switcher - Reading and Writing External IEEE HDD's

    Greetings,
    i'm trying to read and write with my external ieee drive on windows and the mac pro. it's format is ntfs, and is read only on the mac pro.
    i am admin and can't seem to change the permissions to read / write.
    the over all idea is i need to go back and forth between operating systems reading and writing from both sides of the house.
    here's a snapshot.
    http://briankross.com/images/500GBIEEEHDD.jpg
    Mac Pro   Mac OS X (10.4.8)  

    Unfortunately, no. In order to convert the drive to FAT32 it must be re-partitioned which is a destructive process.
    Why reward points?(Quoted from Discussions Terms of Use.)
    The reward system helps to increase community participation. When a community member gives you (or another member) a reward for providing helpful advice or a solution to their question, your accumulated points will increase your status level within the community.
    Members may reward you with 5 points if they deem that your reply is helpful and 10 points if you post a solution to their issue. Likewise, when you mark a reply as Helpful or Solved in your own created topic, you will be awarding the respondent with the same point values.

  • Whats the best way to transfer all my pic and vid files from Mac Air to back up drive to free up space on hard disk?

    whats the best way to transfer all my pic and vid files from Mac Air to back up drive to free up space on hard disk?

    Get a third drive if needed, open the Time Machine pane of System Preferences, set it not to exclude that drive, and drag the files to it. If you're moving an iTunes or iPhoto library, launch the application with the Option key held down and choose the moved library.
    (118789)

  • Reading and writing a ByteArray

    This question was posted in response to the following article: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d54.html

    I believe that the example of Reading and Writing Objects needs a rewrite
    First, the attempt to use comments to interleave a Flex-ified example with a Flash example of the uses of the ByteArray Class is a shortcut that ultimately fails.  Take the time to create two, separately valid examples for the two different frameworks.
    Second, the interlinea comments show a rather poor example of how to write clean code, and are entirely confusing to the person you you trying to help learn how to use your library.  Look at the declaration of a variable called outFile:File, a comment about some function that is to be declared somewhere with the same name, outFile(), and indeed, if this code were to compile -- which I am sure it could not -- the compiler would see that you try to invoke an outFile() function.  No one gets through an interview who writes code that way.
    Third, I am not sure you understand how the readBytes and writeBytes methods work. In the upper example, where an order is being written to the file system, the writeBytes method uses data.length correctly.  At the point in time that it is used, the XML object will have been written into the bytes:ByteArray, so it will have a proper length.
    On the other hand, in the second half of the example, where the readBytes method similarly passes data.length, the reader has to be very lucky to note that at that point in time data.length will have a value of zero.  The inBytes:ByteArray was only instantiated, not populated with anything, as, indeed it should not be, at that point in the logic flow.  Most importantly, when the program does need to readBytes, there is no way for the program to know in advance what the length of the byte stream coming in from the file system will be.  Your example would be more correct, and could make a useful point in showing that readBytes (data) is the correct way to pass the arguments because the default behavior will then be used and the default behavior will be to make data whatever length it needs to be in order to hold the content from the file system.  The program can know the value of data.length after the read has taken place, but not before.

  • When will Apple have a searchable text reader and upload file capability?

    When will Apple have a searchable text reader and upload file capability? I have a large text file I need to upload to my iPhone, search, and dial.
    Anything planned?

    I had a Palm the I used for work and travel it had a very nice application called DOCUMENTS TO GO. It allowed you to upload, view, and edit .doc, .xls, and .ppt files. this was very handy when traveling. Also, I could sync my palm with my iMac and work PC using bluetooth, took care of the file transfers as well. I would like to have the same capability on my iPhone. I bought it to replace all of my old stuff i need it to the same as my old stuff. I hope that Apple reads this and gets on the ball. Until then I still have to carry my Palm or thumb drive on trips.

Maybe you are looking for

  • Wired Connection Set Up

    Just bought an Apple TV and want to set it up via a wired connection.  I have my PC connected directly to my modem so do I have to also connect my ATV directly to my modem OR can I connect it to my PC. Thanks in advance guys, any help with this will

  • Capacity in scheduling agreement

    Hi, The documentation says we can assign transportation capacity and vendor capacity in scheduling agreements in ECC. Can you tell me which field in scheduling agreements are related to  transportation capacity and vendor capacity? I am new to schedu

  • Trying to install WSUS role on Windows Server 2012 R2 using dedicated SQL Instance with static port on remote SQL Server 2012 SP1 CU7 on Windows Server 2012 R2.

    I am trying to install WSUS role on Windows Server 2012 R2 using dedicated SQL Instance with static port on remote SQL Server 2012 SP1 CU7 on Windows Server 2012 R2. It verifies the connection and then throws the error: The request to add or remove f

  • Intel VT support?

    I have Lenovo V100 laptop which has Intel T5600 processor. I have 32-bit Arch and I try to run 64-bit guest OS in VMWare but it is complaining about disabled VT (intels Virtualizion Technology) support. I don't find any switch on BIOS, is there essen

  • Snow Leopard X Server

    Can the Snow Leopard X Server be installed on a iMac, and then a user use the same iMac for their own use without using any of the server os. Also, will a MacBook Pro connect and work to the network. I ahve a small company and want to connect a MacBo