Welcome to Dream.In.Code
Become a Java Expert!

Join 149,586 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,778 people online right now. Registration is fast and FREE... Join Now!




Simple File Output Question

 
Reply to this topicStart new topic

Simple File Output Question, Cant find answer Anywhere!!

UnderTheBridge
6 Oct, 2007 - 01:07 AM
Post #1

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
I need to output to a file that probably wont exist so all I need to know is how to create the text file of name 'name' in the working directory.

It must be so simple that nobody puts it anywhere on the internet cuz I can't find it.
User is offlineProfile CardPM
+Quote Post

UnderTheBridge
RE: Simple File Output Question
6 Oct, 2007 - 01:37 AM
Post #2

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
ignore

This post has been edited by UnderTheBridge: 6 Oct, 2007 - 03:26 AM
User is offlineProfile CardPM
+Quote Post

chuck87
RE: Simple File Output Question
6 Oct, 2007 - 02:45 AM
Post #3

D.I.C Head
**

Joined: 7 Sep, 2007
Posts: 65


My Contributions
I believe this is what you are looking for.The output in this file is a string.

CODE

import java.io.*;

public class Program1
{
    public static void main(String args[])
    {
        try{
            FileOutputStream name=new FileOutputStream("name.txt");
            BufferedOutputStream buff = new BufferedOutputStream(name);
            DataOutputStream data = new DataOutputStream(buff);
            int x=1020;
            data.writeChars("xsf");
            data.close();
        }catch(FileNotFoundException fne){
            System.out.println("File not found in current working directory");
        }catch(IOException ioe){
            System.out.println("IO exceptioin");
        }
    }
}

User is offlineProfile CardPM
+Quote Post

UnderTheBridge
RE: Simple File Output Question
6 Oct, 2007 - 03:25 AM
Post #4

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
Thanks for the reply, Im so use to busy forums that I freak out when I don't get replies. I can no longer add anything to my assignment but I am still interested.

I see that you are just catching the exception but I don't believe anything in there is creating a text file in the working dir, or am I mistaken? What I needed was a way to make an empty text file in the working dir since for that assignment it was pretty much 100% chance the file would not yet exist, then I would write to it as you have shown.
User is offlineProfile CardPM
+Quote Post

chuck87
RE: Simple File Output Question
6 Oct, 2007 - 03:32 AM
Post #5

D.I.C Head
**

Joined: 7 Sep, 2007
Posts: 65


My Contributions
Well after executing the program I had a text file named 'name' in the working directory
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Simple File Output Question
6 Oct, 2007 - 08:37 AM
Post #6

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Nowhere on the Internet? What Internet are you talking about because a quick Google search pulls up multiple results for me.
User is offlineProfile CardPM
+Quote Post

UnderTheBridge
RE: Simple File Output Question
6 Oct, 2007 - 12:19 PM
Post #7

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
I tend to put "s around my google searches so less garbage comes up. I guess in my rush to get done I ignored some of the simpler searches. I believe I used the words "text file" in most of my searches. Or "creating a file in java".
User is offlineProfile CardPM
+Quote Post

sl4ck3r
RE: Simple File Output Question
6 Oct, 2007 - 07:49 PM
Post #8

D.I.C Head
Group Icon

Joined: 22 Sep, 2007
Posts: 119


Dream Kudos: 25
My Contributions
or you could have looked in our snippets section:
http://www.dreamincode.net/code/snippet1305.htm
there are several tutorials on how to do it in there.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month