Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:37:36+00:00 2026-06-10T18:37:36+00:00

File f = new File(filePath); RandomAccessFile rf=null; String firstLine=; if (f.isFile() && f.length() >

  • 0
File f = new File(filePath);

    RandomAccessFile rf=null;
    String firstLine="";
    if (f.isFile() && f.length() > 0) {
        rf = new RandomAccessFile(f, "r");

            rf.seek(0);
        firstLine = rf.readLine();
        rf.close();

Above code is used to read from a file in java constructor . Same file could be opened by different threads at the same time . Now i am making the code in C# and the File class like the one in java is not present . I want to ask what does the constructor of File class is doing here and how different is it from the method in which we simply use following.

StreamReader sr = File.OpenText(path)

And why is random access file actually used when only line to be read from file is the first line.
as there are different threads it could be possible that when one thread is reading or writing in the file at the same time other thread would be reading the file as well . How can we allow it in C#

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:37:38+00:00Added an answer on June 10, 2026 at 6:37 pm

    I think this would do the trick:

    System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
    string firstLine = file.ReadLine();
    file.Close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

String mimetype = .docx\tapplication/vnd.openxmlformats-officedocument.wordprocessingml.document; File file = new File(FilePath, filename); Intent intent = new
Hi I have some code that uses block RandomAccessFile file = new RandomAccessFile(some file,
I'm trying to read a log file of log4net: FileStream fs = new FileStream(filePath,
I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
File file = new File(file-type-string-i-want-2000-01-01-01-01-01.conf.gz); Matcher matcher = pattern.compile(\\-(.*)\\-\\d{4})).matcher(fileName); StringBuilder sb = new StringBuilder();
In my global.asax.cs file. I add an entry routes.MapRoute( Static text, Static/General/{filePath}, new {
I'm deleting a file as such File fileToDelete = new File(filepath); Boolean fileDeleted =
File f = new File(fileExportPath); // D:/temp if(!f.exists()) { f.mkdir(); } f.setWritable(true); f.setExecutable(true); String
I have a piece of code that goes like this File file = null;
I have created a new file using the code below. But if the file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.