Read or open files that comes after extends keyword

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 821 Views - Last Post: 28 February 2016 - 05:50 PM Rate Topic: -----

#1 MarNa12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Read or open files that comes after extends keyword

Posted 27 February 2016 - 05:48 PM

Hi ,all
in this code ,i read filename in package filename using path ,,now i want to read the file B.java that also exist in package filename and relationship between them is inhretance.. "extends" keyword.
in this (public class filename extends B ),
but not write B.java in path ,i want to read any file after extends keyword in program filename.
can anyone help me.
i mean i want to read any file.java that come after extends keyword.

public class  CyclomaticComplexity{
  public static void main(String[] args) {
 try {
 fileName = "C:\\NetBeansProjects\\filename\\src\\filename\\filename.java");
  FileReader fr = new FileReader(fileName);
  BufferedReader br = new BufferedReader(fr);
  line = br.readLine();
            while (line != null) {
            line = br.readLine(); 
        }
        }
        catch (IOException e) {
         e.printStackTrace();
         }}   }
     //////////////////////////////
     ///filename program is
     public class filename extends B
{public static void main(String[] args) {
     int s=9;
     s=s+1;
     }......


This post has been edited by ndc85430: 27 February 2016 - 11:39 PM
Reason for edit:: Fixed broken code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Read or open files that comes after extends keyword

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,436
  • Joined: 25-December 13

Re: Read or open files that comes after extends keyword

Posted 27 February 2016 - 05:54 PM

Please post the code in code tags:
Posted Image
Was This Post Helpful? 1
  • +
  • -

#3 MarNa12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: Read or open files that comes after extends keyword

Posted 27 February 2016 - 05:59 PM

public class  CyclomaticComplexity{
  public static void main(String[] args) {
 try {
 fileName = "C:\\NetBeansProjects\\filename\\src\\filename\\filename.java");
  FileReader fr = new FileReader(fileName);
  BufferedReader br = new BufferedReader(fr);
  line = br.readLine();
            while (line != null) {
            line = br.readLine(); 
        }
        }
        catch (IOException e) {
         e.printStackTrace();
         }}   }
     //////////////////////////////
     ///filename program is
     public class filename extends B
{public static void main(String[] args) {
     int s=9;
     s=s+1;
     }......


////also file B is in file A///
////how can I read all these files comes after extends keyword////

This post has been edited by ndc85430: 27 February 2016 - 11:39 PM
Reason for edit:: Fixed broken code tags

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,436
  • Joined: 25-December 13

Re: Read or open files that comes after extends keyword

Posted 27 February 2016 - 06:08 PM

Please post ALL of your code in code tags. See post#2
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1029
  • View blog
  • Posts: 3,959
  • Joined: 13-June 14

Re: Read or open files that comes after extends keyword

Posted 27 February 2016 - 11:47 PM

I've fixed the code tags for you this time, but please learn to do it yourself for next time.

Your question is not particularly clear. If what you're really asking is how to find the superclass of a given class, then I suggest looking at the java.lang.Class class. You can find information about it in the Java API documentation.
Was This Post Helpful? 0
  • +
  • -

#6 MarNa12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 05:13 AM

I need to know how can I read nested files using one function ...
Can I call function to do that in every
reading of files that comes after extends keyword (nested classes)??
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,436
  • Joined: 25-December 13

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 05:16 AM

Quote

read nested files

I'm having trouble understanding what you are trying to do.
What do you mean by "file"? Is that the same as what the OS defines as a file?
What is a "nested file"? Is that like a "file" inside of a jar or zip file?

Can you post an example of a "nested file" and show what part of it you want to read?

This post has been edited by NormR: 28 February 2016 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#8 MarNa12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 01:11 PM

I mean classes (nested classes)...for example: public class B extends A ... class C extends B.... and so on ...how can i access to each of these classes that comes after extends keyword and read it....
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,436
  • Joined: 25-December 13

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 01:22 PM

Are you talking about lines of code in a source file?
If a source file contains multiple class definitions, are you trying to read lines from the file that are for some particular class?
Basically have a loop that reads the lines from the file.
Have logic inside of the loop that looks at what is on the line it just read and makes a decision about what to do.
If the program wants to access several lines after a particular line has been found, it would need to set a flag (often a boolean variable is set true) so the program would know where it was in the file. When the end of the sequence of lines is found, then the flag would be changed.

This post has been edited by NormR: 28 February 2016 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#10 MarNa12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 03:22 PM

yes exactly ...i do this but i have problem and i dont know how to solve it ....
when i read file that contai class (i.e. public class filename extends B)
i put condition if contain extends so open B and if B contain extends to another class so open it and so on ....
when read it in every time i must rewrite this section :
rerite it more than once!! this is not practical because i have many classes not 3 or 4...
try {
FileInputStream in = new FileInputStream(C:\\\\\\\\......B.java);
FileInputStream inn = new FileInputStream(C:\\\\\\\\......A.java);


FileOutputStream out = new FileOutputStream(C:\\cyclomaticcomplexity\\src\\cyclomaticcomplexity\\cycltestex.java");

byte[] buf = new byte[1024];
byte[] buf2 = new byte[1024];

int i = 0;
while ((i = in.read(buf)) != -1 && ((i = inn.read(buf2)) != -1 )){

out.write(buf, 0, i);
out.write(buf2,0,i);
}

in.close();
inn.close();
out.close();

} catch(IOException e) {
System.out.println("Error copying file");
Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,436
  • Joined: 25-December 13

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 03:30 PM

Quote

i put condition if contain extends so open B

Is there more than one file? I thought you said all of the java source statements were in one file.
I am having problems understanding what you are trying to do. Can you post an example of the input for the program and what you want to do with that input?
Post small and complete examples. No ...s
Was This Post Helpful? 0
  • +
  • -

#12 MarNa12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 03:59 PM

 try {
            String basefile= "C:/NetBeansProjects/filename/src/filename/filename.java";

            String filname="filename.java";
            String filepath = "C:/NetBeansProjects/filename/src/filename/";
            fileName =filepath+filname;
        
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
             // il.size();
             line = br.readLine();

                 //System.out.println(br);        
           
            
            while (line != null) {
                  
           StringTokenizer stTokenizer = new StringTokenizer(line,"( {");
              
           
             while (stTokenizer.hasMoreTokens()) {
                 
                    
              words = stTokenizer.nextToken();
             
             if (words.equals("extends"))
             {   // u= line.indexOf("extends");
               u = line.indexOf("extends"); 

            if (u > -1) 
            {
              
                //System.out.println("Word is at position " + indexfound );
               String exten =(line.substring(u+ex.length()).replace('{',' ').trim()+".java");
                    
             System.out.println(exten);
             

             ext=exten.trim();
            }
             try {
            
                  fileName1=filepath+ext;
                 
            System.out.print(fileName1);
            FileReader frr = new FileReader(fileName1);
            BufferedReader brr = new BufferedReader(frr);
            // il.size();
            String lin = brr.readLine();
            //System.out.println(br);        
           
            
            while (lin != null) {
           //if(lin.contains("extends"))
                int indexfound = lin.indexOf("extends"); 

            if (indexfound > -1) 
            {
                System.out.println("Word is at position " + indexfound );
                fileName2 =filepath+(lin.substring(indexfound+ex.length()).replace('{',' ').trim()+".java");
                      
             System.out.println(fileName2);
               
           
            fi[0]=fileName1;
             fi[1]=fileName2;
             
           for(int i=0;i<fi.length;i++) {
         System.out.println(fi[i]);
           }
                
            }
             System.out.println(lin);
           
           
            
       lin = brr.readLine(); 
        }}
        catch (IOException e) {
         e.printStackTrace();
        
         }
             
             
             
             ///////////create empty file
   
    File file = new File("C:\\cyclomaticcomplexity\\src\\cyclomaticcomplexity\\cycltestex.java");

   boolean emptyCreated = false;
     try
     {
       emptyCreated = file.createNewFile();
             }
     catch(IOException e)
     {
       System.out.println("Error while creating a new empty file :" + e);
     }
   
     System.out.println("Was file " + file.getPath() + " created ? : " + emptyCreated);
     
       //System.out.println(file.getParentFile()+"marwa");
       try {
            FileInputStream in = new FileInputStream(fi[0]);
           FileInputStream inn = new FileInputStream(fi[1]);


            FileOutputStream out = new FileOutputStream("C:\\cyclomaticcomplexity\\src\\cyclomaticcomplexity\\cycltestex.java");
       
            byte[] buf = new byte[1024];
           byte[] buf2 = new byte[1024];

            int i = 0;
            while ((i = in.read(buf)) != -1  && ((i = inn.read(buf2)) != -1 )){

                out.write(buf, 0, i);
                out.write(buf2,0,i);
            }
            
            in.close();
            inn.close();
            out.close();
        } catch(IOException e) {
            System.out.println("Error copying file");
             
             
     
        }}
           
              
              else 
             words =words;
             System.out.println(words);
                   
                 
                       
             
   
          

             }  

          line = br.readLine(); 
        }
       System.out.println(" ");
   
        }
        catch (IOException e) {
         e.printStackTrace();
        }
////////////////////////////////////////
code of filename is
public class filename extends C 
//
{
 public  int d(int s) 
   /*int mm;*/
{   if(s==9)
   return s;
else
  return 0;
}
  
//////////////////////
code of C is:
package filename;
 class C extends B {
public void f3()
    {
        System.out.println("s3 ");
    }
////////////////////////
code of B is:
package filename;

class B extends A { //mmmmm

     public void f2()
    {
        System.out.println("s2 ");
    }
//////////////////////but in every time i must read in previous way ?? can i open it in other way//

  

Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,436
  • Joined: 25-December 13

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 04:02 PM

What files are the lines 163 though line 184 in? Are they in separate files or in one file as shown in the post?

Is this how the files are?

In file Filename.java:
public class Filename extends ClassOne 
//
{
 public  int d(int s) 
   /*int mm;*/
{   if(s==9)
   return s;
else
  return 0;
}
  


In file ClassOne.java:
//////////////////////

package filename;
 class ClassOne extends ClassTwo {
public void f3()
    {
        System.out.println("s3 ");
    }



In file ClassTwo.java:
package filename;

class ClassTwo { //mmmmm

     public void f2()
    {
        System.out.println("s2 ");
    }



Given those three files, what do you want the program to do with their contents?

This post has been edited by NormR: 28 February 2016 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#14 MarNa12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-February 16

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 04:37 PM

The filename file in the package filename.. is the base file that i read it.....
the C and B classes are in the package filename also and associated with filename file through extends keyword ...

They are seperated files but i write them to understand my problems...

exactly ...
in your previous reply this is what i mean exactly.....i hope you help me in this problem....thank you very much.
Was This Post Helpful? 0
  • +
  • -

#15 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,436
  • Joined: 25-December 13

Re: Read or open files that comes after extends keyword

Posted 28 February 2016 - 04:45 PM

Please explain what you want to do with the contents of those three files.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2