Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Basic java tutorial

1,626 views

Published on

Basic java tutorial: How to write to a file

Basic java tutorial

  1. 1. Basic Java TutorialHow to write to a file.
  2. 2. You know when they say“One does not simply understands Java” ?Well its not that hard.You see, I´ve been working with files lately and a friend ofmine had an hard time understanding the concept of writingand reading files in java. So this one goes out not only for him but also for everyonehaving trouble with this concept.Enjoy and hopefully you will find all the answers in thispresentation.
  3. 3. Imports• First of all, we will be using resources from the ‘java.io package’, so go ahead and import this at the TOP of your code.Instead of using ‘*’ you can specify what youreally want to import but for now it works justfine.
  4. 4. Building the program• Now we will need a class and a ‘main’ method.
  5. 5. Recipe to write• Youll now need the following:-A File object – It creates a file if you don’t have one already-A FileWriter object – The output stream. The one who writes.-A BufferedWriter object – Sends data to the output streamNow you might ask “Why do we create the one who sends data (BufferedWriter) to the output stream (FileWriter) first?” Basically because BufferedWriter receives the output stream as a parameter so you need to create it first.
  6. 6. • We do it like this :
  7. 7. Finally, writing !Access your writers write method.Java API already has a method for this. <ReferenceOfYourBufferedOutput>.write("Text here");
  8. 8. • The final product will look like this :

×