Java program to read a text file

Please give me the code that reads a text file.
The important thing is that the file could be modified by another program (Python) while the Java program tries to read it. Do I need to use locks? Or is it enough if I use a synchronized function?
dshrenikAsked:
Who is Participating?
 
objectsCommented:
0
 
enachemcCommented:
you need to use locks
synchonized is used for concurrent acces of threads in the Java Virtual Machine
0
 
dshrenikAuthor Commented:
If possible, can u give some sample code? Thanks!
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
dshrenikAuthor Commented:
So both my Python program and Java program need to use locks?
If yes, will they recognize the locks set by the other program?
0
 
objectsCommented:
you need to use a method that both understand

you can try FileLock, but it is platform dependant
http://www.exampledepot.com/egs/java.nio/SetFileLock.html

0
 
dshrenikAuthor Commented:
Suppose I dont use Locks, will I get any errors, or it is just that I wont get the latest values?
0
 
objectsCommented:
generally when you read it you'll whatever the file contains at that point.
depends a bit on the OS though

0
 
CEHJCommented:
A file lock probably won't work. Try using a lock file - it will be portable across your different platforms
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.