Home » Posts tagged 'File'

Tag Archives: File

Concatenate & Buffer File in Java

Concatenate: It is possible to concatenate two or more files and save in a different file.

In java, by using SequenceInputStream class we can concatenate two or more files.

Buffer Files: In java, we can create a buffer to store temporary data that is read from & written to a stream and this process known as i/o buffer operation.

Buffer is  sit between programmer and source/destination file.

Buffer can be created by using following classes:

  • BufferedInputClass
  • BufferedOutputClass

Example: write a program to concatenate two file A & B and concatenated data print on output screen.

Suppose we have a File “A.txt”

Hello students I am aditya.  

 and                         File “B.txt”

Hello  

Output: Hello students I am aditya.                                                                                           Hello

Example: Write a program to copy the content of one file to another file using buffer/BufferReader.                                                                

Suppose we have a File “A .txt”                               

Hello students I am aditya.  

Output:                                              File “B”

Hello students I am aditya.  

Note: If there is no file “B” it will be create automatically.

Example: Write a program to concatenate two file A & B and save the concatenated data on a file C.

Suppose we have a File “A.txt” and File “B.txt”

Hello students I am aditya.  
Hello  

Output:               File “C”

Hello students I am aditya. Hello  

Note: Ifthere is no file “C” it will be create automatically.

File I/O with Primitive Data Type In Java

In java, basic input-output stream provides a method for reading & writing bytes or character in a file. If we want to read-write primitive data typa(such as int , float etc.) than we can use filter classes to filter data in the original stream.

DataInputStream & DataOutputStream are two filter classes used for creating “data streams” for handling primitive data type.

Example: Write a program to write & read a primitive data on a same file.

Character Stream I/O in Java

In a java programming, like a Byte stream Character stream is also used for input-output for 16 bit. But Byte streams are used to perform input and output of 8-bit.

Most common Character streams classes are, FileReader and FileWriter.

 FileReader- FileReader uses for reads two bytes at a time.

 FileWriter– FileWriter uses for writes two bytes at a time.

Example: Write a program, to copy the content of file A into file B using Byte stream class.

Note: if there is only file A in which data is written and no file B then, file B will be create automatically.

Suppose we have a File “A”

Hello students I am aditya.  

Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.

            File “B”

Hello students I am aditya.  

Byte Streams in Java

In java programming, to perform input and output of 8-bit, Byte stream is used.

Most common byte streams classes are, FileInputStream and FileOutputStream.

FileInputStream : FileInputStream is used for reads one byte at a time .

FileOutputStream : FileOutputStream is used for writes one byte at a time

Writing Bytes

Example: Write a program, to write a bytes to a  file using Byte stream class.

Result

checking file A.txt it will show following content.

Note: If there is no file A then, file A will be create automatically

Reading bytes from a file

Suppose we have a file “A.txt”

Hello Students I am Aditya

Example: Write a program, to read a bytes from file A using Byte stream class and print on output screen.

Example: Write a program, to copy the content of file A into file B using Byte stream class.

Note: if there is only file A in which data is written and no file B then, file B will be create automatically.

Suppose we have a File “A”

Hello students I am aditya.  

Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.

File “B”

Hello students I am aditya.  

File and Input Output in Java

In Java programming, to perform the input-output in file handling  Java has a java.io package which contain contains nearly every class you might ever need.

In file handling streams represent an input source and an output destination.

Stream

In java programming, a sequence of data defined as a “stream”.

Simply  there are two type of streams:

  1. InputStream −  To read data from a source InputStream is used.
  2. OutputStream-  To write a data to a destination OutputStream is used.

Stream Classes

In java,  package “java.io”  contains a large number of stream classes and these classes can be divided into two groups:

  1. Byte stream
  2. Character stream

PHP Working with Directories

Handle a directory there are several functions in PHP language.
i. mkdir()
ii. opendir()
iii. readdir()
iv. scandir()
v. rmdir()
vi. closedir()

i. Create Directory
Want to create a new folder using PHP then use mkdir(). Only one argument is required the folder name or folder location where want to create the folder.

Example

ii. Open Directory
Using opendir() function can open a folder.Only one argument is required the folder path that is to be opened.

Example

iii. Read Directory
readdir() is used to read any folder content. Return the all files and sub directory list of the specified folder (what want to read). One argument is required the folder path.
Example

Output

iv.Scan Directory
sacndir() is same as readdir() but there are small difference. sacndir() return the all files and sub folder name as an array. One argument is required the folder path. No need to open folder in this case.

Example

”;
?>

Output

v. Remove Directory
rmdir() function is used to remove or delete any empty folder from the specified location. If the folder is not empty then rmdir() is not work.

Example

vi. Close Directory
closedir() function is used to closed any directory.

Example

PHP File Handing

File handling is need for any web application so FILE HANDLING is an important part of PHP language. Some task to be done file needs to be processed. PHP have many function handling any kind of normal files. (Liketxt, docx, csv, jpg etc)
Handle a file need to know below topics

i. File create
ii. File open
iii. File read
iv. File write
v. File modify
vi. File close
vii. File remove

i. File Create
Create any kind file through PHP use fopen() function.fopen() function mainly use to open a file in different mode.
File modes are r for read, w for write and a for append. In this function require two arguments. First argument is file name and second argument is file mode value. It is always open a file with specific mode but if the file is not present then create a blank file with the file name.
touch() function also create new file. Only one argument is required the file name or file location where want to create.
Example

Output
File is creating in the same directory where the program is executed!

ii. File Open
fopen() function is used to open a file with a specific mode.

Example

Output
File open in read mode.
Note: if file is not present in this location then PHP return a warning. Like, in above example if sample.txt file doesn’t exist in this location then output is “Warning: fopen(sample.txt): failed to open stream: No such file or directory”

iii. File Read
Read any kind of file use fopen() function with r mode(Read mode). There is a another function fread() which is used to store or directly display the opened file contents as a string. fread() function have two argument first the file value (which is open in r mode using fopen()) and second is file size (how much bytes want to read from the file). Want to read all data of the file use filesize() function which return the original file size in bytes. filesize() have only one arguments, valid file name is required.

Example

Output

Another way to read a file:

Explanation
In this example return the same output as the previous one.
feof() function is file end of the file. There is one argument, the file value. Means if the file content is not ended the while loop is continue. Here fread() is only read 1kb data at a time so whenever the file size is to over while loops continue, after complete the reading of the whole file content while loop automatically stop.

Only some data can be read in a large file using fread() function

Output
How do I love thee? Let me count the ways. I love thee to the depth and breadth and height My

Note: Here only 1kb data read from the whole file.

Read online file directly
file_get_contents() is a PHP function which directly read a file from Internet. Only one argument is required in this function the URL value. But it’s also read local server file so without using fopen(), fread() can be read any file data using file_get_contents().
Example

Explanation
Its return the whole data of this URL file in local server.

iv. File Write:
Write any file using PHP the open the file in w mode in fopen(). Write mode. After that use fwrite() function to write content in this file. fwrite() function have two arguments filename where want to write and another the content value which want to write in this file.

Example

Explanation
After executing the program ‘This is a sample file…’ content is written in the ‘sample.txt’ file. But add another content with previous content is not possible in w mode. If execute the program with new content the only new content value is present in the text file previous data will be lost.

After execute this one ‘Add something with old content’ this value only store in the ‘sample.txt’ file.
‘This is a sample file…’ will be replaced by the current content.

v. File Modify
Update file content then open the file in a mode. Append mode.

Example

Explanation
After executing the program ‘This is another sample file…’ content is written in the ‘newsample.txt’ file. In append mode can be modified previous content of the file and add new content in this file.

After execute this one output is ‘This is an another sample file…Add something with old content!’

vi.File Close
After Open a file it must be need to close the file after operation. fclose() is used to close file. Only one argument is there the file value.
Example

Output remain same.

vii File Remove
Remove or Delete any existing file use unlink() function. There have only one argument. The file name or file location/path is required which want to delete.
Example

After execute the program ‘sample.txt’ will be permanently deleted from the folder.

viii Rename File
In PHP language rename any file name use rename(). There have two arguments. First one is the original file name or original file location and second one is new name value of this file with location.
Example

After execute the program ‘sample.txt’ file name will be changed into ‘newfile.txt’.

ix Copying File
Copy one file content to another file use copy() in PHP. There have two arguments. First one is the copied file name or location and the second one is the file name or file location where to want to copy. Simply copy from source to destination first argument is source file name or location and second is destination file name or location.
Example

Output

Usage of file handling
In real life web application there are many usage of file handling. Here is a one example of usage of file handling is downloading. Download online file into PC using PHP.
Example

Output

Done!

Images Used from Pexels Free stock photos
https://www.pexels.com/photo/white-and-yellow-flower-with-green-stems-36764/
https://www.pexels.com/photo/nature-blue-summer-yellow-36770/

Text file used from
http://textfiles.com/stories/3wishes.txt

Displaying directory contents

For listing a directory content in java we use File class and use isDirectory() method to check whether it is directory or not.
if directory then we use listFiles().

Displaying file properties

Checking whether a file or directory

Checking whether the File object exists if exists then check whether it is file of directory to check file existance exists() method of File class is used if it returns true means file or directory exists, at this point we are not confirm existing File object is file or directory.
To check file object isDirectory() method is used this method returns true if file object is directory otherwise it return false.
isFile() method is used to check whether the file object is file or not if it is a file then it returns true else false.

Managed by MochaHost - Fast & Easy WordPress Hosting