Forum

 
menu_l Home Search Members Calendar Help Donatemenu_r Current time: 12-18-2024, 02:01 AM menu_l

Hello There, Guest!
(LoginRegister)

Poll: Did you like this tutorial, and should I make more?
You do not have permission to vote in this poll.
Yes, for sure!
0%
0 0%
Nah man, dont.
0%
0 0%
Total 0 vote(s) 0%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Thread Contributor: t3hp0wnz0rrrReading from a file. Perfect for Java-beginners!

12-26-2011, 07:51 PM, (This post was last modified: 12-26-2011, 08:04 PM by t3hp0wnz0rrr.)
#1
Reading from a file. Perfect for Java-beginners!
Hey whatsup everyone,

This is a pretty simple java example I deceided to write for my fellow members <3
Merry Christmas, btw.

WARNING: WINDOWS ONLY ATM

First of all I shall explain how I make tutorials.
My tutorials are no snippets, they are actually tutorials. I will teach you into detail what each thing does by commenting my code, and giving explaination.

Below is the code you will learn from.
How to?

This is how to compile and run Java files. t3hp0wnz0rr Wrote:Create a new folder.
Name the folder "Basic Java".

Inside the folder, create an empty text file.
Name the file ReadFromFile (MIND THE CAPS!).
Inside place the tutorial contents (below).
Change the extension from .txt to .java
When done, create a new text file.
Name it something like "compile", it doesn't really matter.
Change the extension from .txt to .cmd

Edit the .cmd file.
Inside, put:

Code:
@echo off
javac ReadFromFile.java
pause

Save it, and open the file.
It is now compiling the file.

When it gives no errors, which shouldn't, create a new text file.
Name the file "run" and change the extension to .cmd
Edit the file and put:

Code:
@echo off
java ReadFromFile
pause

Run the .cmd file, and you will see your application's output!

Below the code for the main tutorial:

This is the main tutorial code. Place it inside the file, as told above. Read the comments I placed to understand what we do. t3hp0wnz0rr Wrote:Before you start, make a text file in C:\ named "textfile".
Inside it, write some lines. Make sure you write multiple lines!
Save, and close. You're ready to move on.

Code:
/**
* We need to import things, to make the Java Virtual Machine know what
* resources we will use in this class.
*/
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;

/**
* This is a very basic Java-example where beginning Java-meisters can learn alot from!
* @author Bart
*
*/
public class ReadFromFile {

    /**
     * The main method, called by the Java Virtual Machine when you run the program.
     * If this method is not found, or is not static, it wont be able to run.
     */
    public static void main(String[] args) throws Exception {
        /*
         * Create a new File, with specified name and location.
         * If you do this, the file wont be created, it just makes a variable File.
         */
        File fileToReadFrom = new File("C:\textfile.txt");
        
        /*
         * Construct a File input stream, to read from the file we give it.
         * This reads very carefully, and will never miss data.
         */
        FileInputStream fileStream = new FileInputStream(fileToReadFrom);
        
        /*
         * Create a new scanner.
         * A scanner is basically an instance, which reads from a stream, as we created above.
         * Scanners also never miss data, and scanners can be used for any type of data reading.
         * It also secures the stream not to be parsed any byte futher until the byte has been read/processed.
         *
         * On another note: A scanner can even be used to ask for user input!
         * For example, in the command console you could ask like: What is your name?
         */
        Scanner in = new Scanner(fileStream);
        
        /*
         * Now, we create a while-loop. This loop keeps repeating, until the specified expression is true.
         * For example, the following would keep executing until the time is 25th december, next year:
         *
         * while (System.currentTimeMillis() < 1356390000000){
         *         System.out.println("It's still not christmas yet :(");
         * }
         *
         * Be warned, that while loops, as wel as for-loops stop the current process. This could cause
         * a lot of lag, and even the program to be not responding. Using separate threads (some next tut)
         * solves this.
         *
         * This loop we create, keeps repeating until the next line is null, empty.
         */
        
        while (in.hasNextLine()){
            /*
             * Now, we put the next line into a string.
             * This is important, because if we read a line, it cannot be re-read.
             * The line would be the next in that case.
             */
            String thisline = in.nextLine();
            
            /*
             * Now it is time to show this to the console.
             * We use System.out.println(""); to print an empty line, so
             * System.out.println(thisline); would print thisline!
             */
            System.out.println(thisline);
        }
        
        /*
         * At the end, we inform the user that we are done.
         */
        System.out.println("We are done!");
    }

}

This was my tutorial, thank you very much for reading.
Add Thank You Reply
[-] The following 2 users say Thank You to t3hp0wnz0rrr for this post:
mgmaik, Razor
12-26-2011, 08:10 PM,
#2
RE: Reading from a file. Perfect for Java-beginners!
This is really good! thanks you!
Add Thank You Reply
[-] The following 1 user says Thank You to mgmaik for this post:
t3hp0wnz0rrr
12-26-2011, 08:20 PM,
#3
RE: Reading from a file. Perfect for Java-beginners!
(12-26-2011, 08:10 PM)mgmaik Wrote: You are not allowed to view links. Register or Login to view.This is really good! thanks you!

Thank you too for replying!
Add Thank You Reply
12-28-2011, 10:38 PM,
#4
RE: Reading from a file. Perfect for Java-beginners!
Very good explained. Great job!
Add Thank You Reply
[-] The following 1 user says Thank You to Razor for this post:
t3hp0wnz0rrr
12-29-2011, 09:04 AM,
#5
RE: Reading from a file. Perfect for Java-beginners!
(12-28-2011, 10:38 PM)Razor Wrote: You are not allowed to view links. Register or Login to view.Very good explained. Great job!

Haha, thanks!
Add Thank You Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Implementing Servers in Java RazorDOX 7 8,135 12-26-2011, 07:33 PM
Last Post: t3hp0wnz0rrr

Forum Jump:


Users browsing this thread: 1 Guest(s)