473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to read files

eyeofsoul
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.
Sep 14 '07 #1
7 2549
r035198x
13,262 8TB
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 a file depends on the type of the files you are reading.
How you read text files is different from how you read binary files and different from how you would read .pdf files.
Sep 14 '07 #2
Nepomuk
3,112 Expert 2GB
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.
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.

You might however not want to use the Scanner class, as it interprets what it reads, but FileInputStreams and BufferedReaders instead.

Greetings,
Nepomuk
Sep 14 '07 #3
JosAH
11,448 Expert 8TB
You might however not want to use the Scanner class, as it interprets what it reads, but FileInputStreams and BufferedReaders instead.

Greetings,
Nepomuk
Readers do quite a bit of interpretation as well; better stick to FileInputStreams
for reading raw bytes.

kind regards,

Jos
Sep 14 '07 #4
i 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?
Sep 16 '07 #5
saharf
4
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.
hi. 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.println("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 string
Sep 16 '07 #6
JosAH
11,448 Expert 8TB
i 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?
No, that's not it; at the lowest level every file is just a sequence of bytes; it's us,
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,

Jos
Sep 16 '07 #7
hi. 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.println("content : "+ content);
thanks you all for giving me the picture of reading a file,,hehe..finally i can read anyfile that i want.hehe..this code really help..
Sep 16 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Sebastian Krause | last post by:
Hello, I tried to read in some large ascii files (200MB-2GB) in Python using scipy.io.read_array, but it did not work as I expected. The whole idea was to find a fast Python routine to read in...
2
by: Andrea Bauer | last post by:
Hallo, wie kann ich so eine Datei unter .Net schreiben C++ oder C#. Bitte mit Funktionsaufrufen. Vielen Dank. Grüße Andrea <Product> <ProgramNumber>2</ProgramNumber>
4
by: who be dat? | last post by:
I feel stupid for asking this but I can't figure this out. I've got some text files I want my website to read. The text files are located in a subdirectory of my application. Physically, the...
8
by: Zephyre | last post by:
I have some UTF-8 text files written in Chinese to be read. Now the only method that I know to read text from it is to use fopen() function. Thus, I must read the contents byte by byte, change the...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
7
by: Tracks | last post by:
I have old legacy code from vb5 where data was written to a file with a variant declaration (this was actually a coding error?)... in vb5 the code was: Dim thisdata as integer Dim thatdata...
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
11
by: John Nagle | last post by:
I'm looking for something that can read .MDB files, the format Microsoft Access uses, from Python. I need to do this on Linux, without using Microsoft tools. I just need to read the files once,...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
3
by: maneshborase | last post by:
Hi friends, I am facing one serious problem in my application. I am trying to open dicom image file (.dcm) has size around 400 MB. But I am getting and unhandy exceptions, Some time, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.