Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy

    [TUT]How to read and write to a text file

    NOTE: Sorry I was just reading around and found a "Tutorial" section. I'm not sure if this was suppose to go in there. I'm sorry if i miss-posted this.

    I'm new to this forums, so for my introduction to the forums I made a tut on how to read, and right to a .txt file.

    A majority of what I do on the forums will probably be in the Java section.

    NOTE: NO ONE IS PERFECT, I did have a couple mistakes in my video, please don't spam me with flame, I'm looking forward to a nice stay! :P

    [html]https://www.youtube.com/watch?v=L8DGYs5yMdc[/html]
    [YOUTUBE]<object width="425" height="344"><param name="movie" value="https://www.youtube.com/v/L8DGYs5yMdc&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="https://www.youtube.com/v/L8DGYs5yMdc&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>[/YOUTUBE]

    Sorry the video came out not so good :\ here is the source code if you want to look it over.

    Code:
    import java.io.*;
    import java.util.*;
    public class Reca {
    
    	public static void main(String[] args) {
    		Reca r = new Reca();
    		r.writeFile();
    		r.readFile();
    	}
    	
    	public void writeFile() {
    		File file = new File("dennis.txt");
    		
    		if (file.exists()) {
    			System.out.println("The file already exists!");
    			return;
    		} else {
    			try {
    				PrintWriter pw = new PrintWriter(file);
    				pw.println("Hello, how are you?");
    				pw.println("dennis");
    				pw.println("martinez");
    				pw.close();
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	public void readFile() {
    		File file = new File("dennis.txt");
    		
    		if (!file.exists()) {
    			System.out.println("Can not find the file you are trying to read");
    			return;
    		} else {
    			try {
    				Scanner input = new Scanner(file);
    				
    				while (input.hasNext()) {
    					String s = input.nextLine();
    					System.out.println(s);
    				}
    				
    				input.close();
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    }
    Last edited by XORxHACK; 02-23-2010 at 11:01 PM.

  2. The Following 2 Users Say Thank You to XORxHACK For This Useful Post:

    nepito (03-13-2010),why06 (02-23-2010)

  3. #2
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Lol tried to read this tutorial about ten times, and it kept changing, so I was like: "fine I'll just wait till he's done editing it", but nice tut.

    I didn't know about the whole wildcard thing. That was neat. Strangely enough I remember accessing files differently, but I forget how now... =/

    I learned a little bit of Java in highschool personally, but I've been on this C++ thing so long Im a little rusty, but I'd love to break it out again. Have you tried programming the Android SDK any?

    Oh, and welcome to MPGH! This is the right section.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  4. #3
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy
    Ah android, I've heard about it. I've downloaded the files, then I kind of lost interest because I couldn't figure out how to work with it (fail on my part seeing as I only gave it 2 minutes before I actually gave up). But I did want to start using Android.

    Sorry about the whole updating thing, have to have my first impressions aha.

    I'm not seeing too much on the Java tuts, maybe I can help out and start from basic variables and desicion structors all the way to loading a sql database with java.

    Oh, apart from Java, I know: MySQL, PHP, HTML, CSS, and any language is pretty easy to understand since I have a concept in programming.
    Last edited by XORxHACK; 02-23-2010 at 11:42 PM.

  5. #4
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Quote Originally Posted by XORxHACK View Post
    Sorry about the whole updating thing, have to have my first impressions aha.
    Lol. Its no problem Im the same way.

    Anyway I have to be honest with you this forum is dead. Im the only other active member on MPGH which knows Java as far as I know, and I don't post here often mainly because there no one to talk, and nothing really to work on in Java. Hmmm... one more I think, that's active may be wrong...

    I have a question though... what compiler were you using because it seemed like you were using .bat's to compile and run.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  6. #5
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy
    That was just a plain .bat file(s), here is what was inside of them.

    Compiler
    Code:
    @echo off
    "C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" -d bin -Xlint:deprecation -Xlint:unchecked *.java
    pause
    Run
    Code:
    @echo off
    "C:\Program Files (x86)\Java\jre6\bin\java.exe" -cp bin Reca
    pause
    I usually use JCreator, but I felt the need to use Notepad++ and homemade compilers. XD

    Ah I see, yeah the person who posted the hello mpgh tut, I know him from another forum site I rarely go on. I could talk about programming in general, I just like talking about Java because It's what I like the most out of any language.

    What is more active on this forum? I found this site just looking for CA hacks, then I seen there was a Java programming section, so I had to register. XD
    Last edited by XORxHACK; 02-23-2010 at 11:55 PM.

  7. #6
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Well the most active programming forum is definitely VB, C++ second, kind of dies out from there. I like Java as well, its just so easy to use, but still very versatile. C++ compared to Java can get pretty painful, but I decided to pick it up so I could make hacks and such.

    Oh look at the time! Well time for bed, but good meeting you.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  8. #7
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy
    Yeah, I'm debating on learning C++, I don't want to to overcome my Java knowledge, sounds weird, but it's true.

    Yes! It's very nice to conversate with friendly people instead of what I had before (I won't go into detail). It was very nice meeting you. I'll see ya around forums!

  9. #8
    jeremy6996's Avatar
    Join Date
    Jul 2006
    Gender
    male
    Posts
    154
    Reputation
    10
    Thanks
    7
    Quote Originally Posted by why06 View Post
    Lol. Its no problem Im the same way.

    Anyway I have to be honest with you this forum is dead. Im the only other active member on MPGH which knows Java as far as I know, and I don't post here often mainly because there no one to talk, and nothing really to work on in Java. Hmmm... one more I think, that's active may be wrong...

    I have a question though... what compiler were you using because it seemed like you were using .bat's to compile and run.

    I'm writing a game engine in Java yo. Ofc I can write it. Why is everyone assuming I don't know Java just because I know C\C++. Jesse has done it to me as well :@

    As far as you code goes. If you're going for a modular OO design, everything should be in a class that suits it's function, functions shoudn't depend on anything outside of the class it's in(IK you can't take stuff out of classes in Java, the point is that it should be in a class which suits it's function), and they shouldn't make any assumptions.

    Also, never let a function determine how an error should be resolved. Always leave it up to the caller unless it's critical. For example, let the caller process the thrown exceptions(if it's not relevant to the caller, then just return false), or manage an error(a function returning false usually means an error occured).

    I'm not sure how open-sourced JevaEngine is going to be, but if it does happen to open up, I'll release it here.
    Last edited by jeremy6996; 02-24-2010 at 12:31 AM.

  10. #9
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Oh yeh Jetamay. I wasn't sure... Anyway how's that going? Never got why you wanted to build an engine in Java of all things. :l

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  11. #10
    axel fox's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    60
    Reputation
    10
    Thanks
    1
    My Mood
    Amused
    nice tut and welcome to mpgh

  12. #11
    jeremy6996's Avatar
    Join Date
    Jul 2006
    Gender
    male
    Posts
    154
    Reputation
    10
    Thanks
    7
    Quote Originally Posted by why06 View Post
    Oh yeh Jetamay. I wasn't sure... Anyway how's that going? Never got why you wanted to build an engine in Java of all things. :l
    Less competition(still looking for one similar to mine), and Ima use it for next year when I'm taking Java Programming courses.

  13. #12
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy
    Quote Originally Posted by jeremy6996 View Post
    I'm writing a game engine in Java yo. Ofc I can write it. Why is everyone assuming I don't know Java just because I know C\C++. Jesse has done it to me as well :@

    As far as you code goes. If you're going for a modular OO design, everything should be in a class that suits it's function, functions shoudn't depend on anything outside of the class it's in(IK you can't take stuff out of classes in Java, the point is that it should be in a class which suits it's function), and they shouldn't make any assumptions.

    Also, never let a function determine how an error should be resolved. Always leave it up to the caller unless it's critical. For example, let the caller process the thrown exceptions(if it's not relevant to the caller, then just return false), or manage an error(a function returning false usually means an error occured).

    I'm not sure how open-sourced JevaEngine is going to be, but if it does happen to open up, I'll release it here.
    I'm not quite sure what you mean by let the caller process the thrown exceptions? Are you tring to go twards something like a JOptionPane.showConfirmDialog to continue or something? Could you please explain in a little more detail?

  14. #13
    jeremy6996's Avatar
    Join Date
    Jul 2006
    Gender
    male
    Posts
    154
    Reputation
    10
    Thanks
    7
    Code:
    public class MainClass
    {
        public static void main(String[] args)
        {
            try
            {
                int iReturn = someMethod();
            }catch(Exception ex)
            {
                //Caller is now aware an error occured. And can access the ex obect for more details
            }
        }
    
        public static int someMethod() throws Exception
        {
            throw new Exception("Some error occured"); //Everything past the throwing of an exception is unreachable, thus the compiler shouldn't ask for a return, but some still do(older ones).
        }
    }
    Did it in quick-reply box. So if there are any errors, ignore them.

    The caller is the function which invoked the function being executed.
    The callee is the function be executed by the caller.
    Last edited by jeremy6996; 02-25-2010 at 03:01 AM.

  15. #14
    XORxHACK's Avatar
    Join Date
    Feb 2010
    Gender
    male
    Posts
    15
    Reputation
    10
    Thanks
    7
    My Mood
    Breezy
    Hmm, I'm still not understand why you would do this? Sorry.

    You can just throw an error with catch, no un-wanted code needed. (like I said, I'm still trying to figure how you're trying to put it)

  16. #15
    jeremy6996's Avatar
    Join Date
    Jul 2006
    Gender
    male
    Posts
    154
    Reputation
    10
    Thanks
    7
    Quote Originally Posted by XORxHACK View Post
    Hmm, I'm still not understand why you would do this? Sorry.

    You can just throw an error with catch, no un-wanted code needed. (like I said, I'm still trying to figure how you're trying to put it)
    Alright. Consider my scriping engine takes a term(any term). It could be a variable. Function call or constant. The first thing it does is it tries to covert the term into a constant expression. A number. Currently it is in the form of a String. I catch the number format exception. In this case, the caller isn't going to know what to do with the number format exception. The function enumerating the term does. In this case a try\catch expression is used, if an exception occures it goes on to check if it's a function call or a variable because we know it's not in the correct format to be an constant expression. If it is a variable, it will see if the variable currently exists, and if it does, the return of the enumerateTerm function will be the value of the variable located. If the variable does not exist, a new exception describing the error is thrown, as a return of 'false' is not sufficient enough for the caller because it could be due to a missing variable or function.

    In your code, when there is an error, you don't return false, you don't throw an exception. You leave it up for the callee to solve the error(I.e printing a stack strace in your case.), which should be avoided.

    It's not about the quantity of code, it's about it's quality. There are many short-cuts that can be taken, but it will result in a messy design, which is hard to inhance or change later in development.

    Only throw exceptions if the caller needs more information on an error(i.e long routines), and if the exception being thrown is relvant to the caller. Otherwise, return false for fail, and true for success.
    Last edited by jeremy6996; 02-25-2010 at 10:33 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. need help!? read and write (txt)
    By GER-Domi. in forum Visual Basic Programming
    Replies: 5
    Last Post: 09-10-2010, 08:11 PM
  2. [TUT]How to read input from keyboard
    By XORxHACK in forum Java
    Replies: 3
    Last Post: 05-07-2010, 05:03 PM
  3. Tut how to crossmod and texture mod
    By lol in forum Combat Arms EU Mods & Rez Modding
    Replies: 10
    Last Post: 03-24-2010, 05:19 AM
  4. [Help]Reading and Writing to .txt files on the internet
    By treeham in forum Visual Basic Programming
    Replies: 6
    Last Post: 01-30-2010, 01:07 AM
  5. [Tutorial/Snippet][VB6] Reading and writing INI Files
    By That0n3Guy in forum Visual Basic Programming
    Replies: 6
    Last Post: 10-26-2009, 05:31 PM