Java program to read a text file

Posted on 2010-11-30
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!
LVL 20

Expert Comment

ID: 34236866
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

LVL 92

Expert Comment

ID: 34236868

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 250 total points
ID: 34236976
LVL 86

Assisted Solution

CEHJ earned 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 68
Why is enum singleton a better approach than static factory 3 67
Java class and jar 3 61
Strange router problem - can't access 14 109
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 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