Question 8: "How do I create a file and write to it in Java?"

Java - Top 10 Most Viewed Questions on Stack Overflow Question 8: "How do I create a file and write to it in Java?"
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

The eighth question with over 2.7 million views is how do I create a file and write to it in Java. And as you might suggest, also here there are different approaches to to do this. And the simplest approach, if you want only to create an file in your file system is to use the Create New File method from the class file. What is an instance of file or an instance of the classifier? It isn't necessarily a real file in your file system, but it is an abstract ation of a file or directory in your file system. So you can create a suction instance by passing a name for a file or a path name.

So if you if you want to create a file in a special folder, so you can, you can pass it to the constructor and the name of the file later on. But if you only create an instance it's it doesn't exist necessarily in the file system. So it's only an abstract ation of such an entry in your file system. But if you want to create such an file in the file system, you just have to create first an instance of the file class and then you should call it a create new file. And then after it you it is created, if it is possible, you your your operation system or your file system may may may say, you don't have to write to to write their a file or something like that. There are Many scenarios, because file isn't possible to write out or to create.

So yeah, you method might throw an i o exception. And you get also a Boolean if it worked to create a file or not. But the resize of this method is, as I said, an empty file with the with the size of zero bytes. The question here is, how do I create a file and to write to it in Java, and there are mainly two different approaches. The first one is to use the file output stream class. And the second one is to use the print writer class.

So where's the difference between them both data streams who are writing or which are right To the files and data to the files, so they do the same thing. But the file output stream is on the layer of bytes. So you can you can say this by despite and despite I want to write it to the fire police and to print writer as the name might suggest rights strings or text forms characters to the fire. So if you want to create some text file which the user is able to read afterwards, you can for sure use the file output stream as well because every file is consists of bytes in in the it but it would be right easier to use the print writer because you don't have to manage the encryption and so on. So, the print writer helps you a lot by writing strings to a file or text to a file.

And but sometimes you you only want to create to put only bytes into a file and if you do so, to file output stream is your friend. So, maybe there is a scenario where you get if you you might copy a picture for example. So, you can use file input stream and the file input stream would read bite after bite after bite of this image. So you create an file input stream with a file that points to this image. And then you can read byte by byte by byte from the file input stream and then you pass each each byte you threat to the file output stream and Ryder tried to write it into another file. And if both streams are finished, yeah, you you've copied the image file.

So yeah, an image you couldn't write an image by text for sure. If you want to write an image you have to use bytes. But you can also get the bytes from an from from an network connection or something like this. So file output stream is is your friend to write bytes. How does it work? You get an instance of fire class.

So it is, as I said, it might be only an abstraction to reify. It doesn't have to exist. And if it don't exist, the file the file output stream would create Add your file system first. And there might be there might be exceptions. If you are, for example, your file instance is pointing to a directory and the file output stream isn't for sure isn't able to write data into a directory and if you try to do this you get for example file not found exception. So, let us take a look.

There are many things where the file not found exception may or cure. So we are using the file output stream. And this constructor and as you can see, find out found if the file exists, but it's a directory rather than a regular file or does not exist but cannot be created or cannot be opened for any other reasons. So yeah, there are this three, three opportunity It is why you get in file not found exception during using the constructor. So, but if everything works, the file would be created in your file system. And after it you have a file output stream that is pointing to this file.

And you can then write bytes to it. And you can do this by using for example, the write byte array method. So, you can pass a whole byte array to it and it is written to the file or you can use for example, if you have only one single byte, you can use the write int method and the end one, the INT is meant that you would pass a byte to it. So, let's have a look. This end B is the byte to be written Now, so, this would be a way if you want to write bytes in it. If you have text that you want to create or to if you want to pass text into a file, you could use the print writer, the print writer is working pretty much the same as the file output stream.

So you are passing a file instance to the constructor. You also can get file not found exception for the same reasons. And then you have if you have Once created the print writer instance, you can, for example, use plenty of methods you can use print line a string x, so the string is printed into the file and then the line is broken into a new line. If you don't have want to have a new line after the string, you can use print method. And as you can see, you can also print integers, print long plain floats, Boolean, and so on print object, and so on now, print f print formatted and so on. So yeah, this would be the way to do this if you want to create and write text to on file.

So let, let's take a look at an example of this. The first example would be only the Create New File method from the file class. So as you can, as you can see, we are calling new file with the path name. It's just using the root directory from from your Java application. So to five would named empty phi and you create a new file method and to recite is simply creates an empty file with a size of zero bytes. If you are using file output stream, it could look and this like this new file output stream you pass an instance of new file with the file output stream test as path name and then you create an file output stream.

This kind of try catch stage statement is called try resource statement. So every stream has to be closed at the end and if you are using the file added to try Rif resource statement, you don't have to, you don't have to close the stream at the end on your own because the try A resource statement closes the stream so it's very elegant to use this and yeah, you create in file output stream called out and we create a byte array with the size of five and rewrite despite array to the stream and to recite would be a file with size of five bytes. And in the last example, the last example would be the printwriter also with a new file instance print writer test. And as I said to print writer is to use it for to to use to print text and text might be not every single char is not one byte at the size of one byte and so on.

It depends what character encoding you are using and so on. So the print writer has many opportunities to to set encodings and so on. So it's very elegant to use it if you want to, to have strings. And so, if you only create certain printwriter it uses the default encoding and so on. And what am I doing here is first of all I am writing a string hello after it I only print or break the line into a new line. After that, I have the string word I create a character and stream of characters out of it.

And on this stream, I am calling for each method and for every character in the in the string word. It is written with the right print line and then I convert the character to a string and to recite what looks like this. So you have the world Hello, world. Hello and I After it, the word world, and for each character in each in a new line. So the resize is 16 bytes. But we have only How many?

How many characters 1-234-567-8910. And a few line breaks. But as you can see, there are more bytes and characters and line breaks and so on because the encoding and yeah, as I said, each character is maybe some characters, bigger than one byte, some characters has decide the size of one byte and so on. But you don't have to be able to do this on your own because the printwriter helps you and the print writer as your friend by using texts or to passing texts and files. And yeah, that's the way you can create and write data, bytes or text in a file.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.