-1

I'm trying to read a text file line by line within my HTML/Javascript web page.

The text file I want to read is in the "data" folder in the web page directory. To read the text file, I'm using FileReader and BufferedReader within my script tags.

I'm getting an error "Uncaught syntax error: Unexpected identifier" on the line where I create the FileReader with the file path, see code below.

I thought the problem might be that these were Java objects so perhaps I needed to import those. I tried to add:

import java.io.FileReader;
and
import java.io.BufferedReader;

but then got an

"Unexpected identifier"

error for

"import java.io.FileReader;

"

FileReader tidewatchDataOutput = new FileReader("data/test.txt");
BufferedReader bufferSCHISMDataReader = new BufferedReader(tidewatchDataOutput);

The web page is no longer loading properly in Chrome as it did before; it just stops in the middle.

jkdev
  • 11,360
  • 15
  • 54
  • 77
dschatt
  • 3
  • 1
  • 8
    java != javascript – freefaller Apr 03 '19 at 15:20
  • 1
    Can you confirm if you're trying to do this in `Java` (and therefore you've picked the wrong tag)... or you really are trying to do it in `javascript` and have misunderstood the difference between the two languages? My gut feeling is it's the former – freefaller Apr 03 '19 at 15:41
  • I don't agree that this is a duplicate of the linked question. – Reinstate Monica -- notmaynard Apr 03 '19 at 15:42
  • 4
    Looks like the OP was trying to solve a problem in JS, didn't understand the difference between JS and Java, and followed a Java tutorial … which didn't work. It's *possible* that they are using JSP and got confused between `<%` and ` – Quentin Apr 03 '19 at 15:57
  • Either way, the question should remain closed because it either is a duplicate or it is unclear. If they clarify it and the duplicate is not applicable then it can be reopened (or, more likely, have the duplicates changed). – Quentin Apr 03 '19 at 15:58

1 Answers1

0

Browsers support Javascript only*, not Java. Although there is Java in both names, they're 2 completely different languages. One has nothing to do with the other.

You're using Java instructions to do it. So you will get nowhere. Javascript until recently didn't support reading files in the client. HTML5 came to the rescue and now you can. Here is an explanation on how to read files using javascript:

https://www.html5rocks.com/en/tutorials/file/dndfiles/

*there's one thing called applets in Java which allow to run Java in a browser, client side but they are deprecated and are removed from Java since version 9, so forget about running Java in the client.

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73