Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Java program to read a text file

Posted on 2010-11-30
Medium Priority
Last Modified: 2012-08-14
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?
Question by:dshrenik
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 34236849
you need to use locks
synchonized is used for concurrent acces of threads in the Java Virtual Machine

Author Comment

ID: 34236850
If possible, can u give some sample code? Thanks!
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 34236876
So both my Python program and Java program need to use locks?
If yes, will they recognize the locks set by the other program?
LVL 92

Expert Comment

ID: 34236927
you need to use a method that both understand

you can try FileLock, but it is platform dependant


Author Comment

ID: 34236940
Suppose I dont use Locks, will I get any errors, or it is just that I wont get the latest values?
LVL 92

Expert Comment

ID: 34236970
generally when you read it you'll whatever the file contains at that point.
depends a bit on the OS though

LVL 92

Accepted Solution

objects earned 1000 total points
ID: 34236976
LVL 86

Assisted Solution

CEHJ earned 1000 total points
ID: 34237511
A file lock probably won't work. Try using a lock file - it will be portable across your different platforms

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question