hello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
how to read files
Collapse
X
-
How to read a file depends on the type of the files you are reading.Originally posted by eyeofsoulhello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
How you read text files is different from how you read binary files and different from how you would read .pdf files. -
As you're working on encryption, I guess you will want to read and write the files in binary format. To get you started, this Article should give you a rough idea.Originally posted by eyeofsoulhello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
You might however not want to use the Scanner class, as it interprets what it reads, but FileInputStream s and BufferedReaders instead.
Greetings,
NepomukComment
-
Readers do quite a bit of interpretation as well; better stick to FileInputStream sOriginally posted by nepomukYou might however not want to use the Scanner class, as it interprets what it reads, but FileInputStream s and BufferedReaders instead.
Greetings,
Nepomuk
for reading raw bytes.
kind regards,
JosComment
-
hi. here is a way that you can read the content of a file, but before you should specify your file nameOriginally posted by eyeofsoulhello..can anybody give me a guide on how to read any file using java..i am working in encrypting files..any file..can anybody give a hand.
FileInputStream fis = new FileInputStream ("E:/Sahar/source codes/exam.txt");
int num = fis.available() ;
byte b[] = new byte[num];
fis.read(b);
String content = new String(b , 0 , num);
System.out.prin tln("content : "+ content);
in the 1st line, you shud write d name of file u want work on it
in d 2nd line: u r asking to find d number of bytes that can b read from fiel
in d 3rd line: making a byte array with d size of ur file
.......: reading byte by byte of file
.......: making a string to write d result of reading file
.......: and finally writing d result as a stringComment
-
No, that's not it; at the lowest level every file is just a sequence of bytes; it's us,Originally posted by eyeofsouli think i got the idea.first i must convert the file into binary right?!but how do i convert the files into binary?..for example i want to convert a jpeg file into binary.how can it be done?
humans who want to interpret the sequence of bytes, e.g. if all of them represent
printable characters and newline (and/or carriage return) characters, it's us humans
that call such a sequence a 'text file'. If the sequence has some other structure,
defined by us, we might call them jpeg files etc. etc.
A FileInputStream simply reads those bytes from a file and that's the class you
need if you just want read bytes from a file sequentially.
So you don't 'convert a file to binary' because every file already is a binary thing.
kind regards,
JosComment
-
thanks you all for giving me the picture of reading a file,,hehe..fin ally i can read anyfile that i want.hehe..this code really help..Originally posted by saharfhi. here is a way that you can read the content of a file, but before you should specify your file name
FileInputStream fis = new FileInputStream ("E:/Sahar/source codes/exam.txt");
int num = fis.available() ;
byte b[] = new byte[num];
fis.read(b);
String content = new String(b , 0 , num);
System.out.prin tln("content : "+ content);Comment
Comment