-2

I am not new to Java programming but to Javascript. I am facing a problem especially in dealing with files in javascript. For your reference i have written a piece of code in java and i want to see it in javascript. Please help me in this regard.

File file1= null;
String result = null;
 File folder = new File(folderPath);
    File[] filesList = folder.listFiles();
    for(File f : filesList) {
        if(f.getName().equals("focus")) {
            for(File f1 : f.listFiles()) {
                if(f1.getName().contains("xxx")) {
                    file1= f1;
                    break;
               }  
            }
        }
    }
FileInputStream fis = new FileInputStream(file1);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while((line = br.readLine()) != null) {
  if(line.substring(10,12).equals("AB")) {
    result="found";
    break;
  }
}
System.out.println(result);
user3687429
  • 29
  • 1
  • 2
  • 3
    java and javascript are completely unrelated technologies. why would you want to do something like this in javascript? – Stultuske May 29 '14 at 12:16
  • Moreover javascript is client side, do you really intend to read the file on the client machine? there are a few things available in html5 that would allow you to interact with the file using javascript but those will not be anywhere close to java. – ganaraj May 29 '14 at 12:18
  • @ganaraj technically, the FileApi isn't bad. But it won't be possible to read file or access the filesystem directly. – Loïc Faure-Lacroix May 29 '14 at 12:20
  • @Stultuske i dont have any real requirement to do above piece of code in javascript, but i want to dig deep in javascript regarding file handling. – user3687429 May 29 '14 at 12:25
  • 1
    then you shouldn't be looking into Java code. they're meant for different purposes, and they have different possibilities. – Stultuske May 29 '14 at 12:26
  • so you meant to say we cannt read any file data at client(browser) side using javascript? – user3687429 May 29 '14 at 12:28
  • I think someone woke up grouchy today and decided to downvote the hell out of everything ;) – codebox May 29 '14 at 12:44

2 Answers2

-1

If you are running your JavaScript in a web browser then you won't be able to do this - for security reasons JavaScript in browsers does not have direct access to the file-system.

Would you want some website that you visit to be able to look through the files on your local PC? - probably not.

If you are running your JavaScript in a non-browser environment (eg node.js) then there are APIs that you can use to do this, but they aren't part of the JavaScript language itself.

codebox
  • 19,927
  • 9
  • 63
  • 81
  • Thnx codebox, But i heard that we can handle files in javascript using "FileSystemObject", at what extent we can impliment above logic in javascript> – user3687429 May 29 '14 at 12:21
  • handling files and accessing the file system is not the same – Stultuske May 29 '14 at 12:26
  • @Shryme - thanks, I'm aware of the FileReader API but the code above needs to get a directory listing which isn't supported by this (with good reason) – codebox May 29 '14 at 12:38
  • @Stultuske - that's true, but to access a directory listing as in the code above you do need to talk to the file system – codebox May 29 '14 at 12:39
-1

In JavaScript you can use this new standard API for doing some reading operation with files.

xdevel2000
  • 20,780
  • 41
  • 129
  • 196