Read file header in java

How to include Header File (.h) in Java

Question: I am trying to read file headers using java, I want to get file type(image, audio whatever). Solution 3: Briefly, in C or C++ header files allow different files to share common definitions or declarations.

How to include Header File (.h) in Java

I want to use this which says to use a particular method I have to include tcutil.h in my java code. Can anybody help me, how to do that ? Another point: we can easily create an header file and include it in to C code but why reverse is so hard (means lots of work have to do) ? May be stupid, but little bit hints will be helpful.

This might be more complicated than you think. The .h files are C language include files which usually describe the methods and data structures of a C-library. You would have to Java Native Interface (JNI) in order to include these libraries in your java code.

You have basically two options

There’re already java-bindings available.

You can’t include C/C++ headers into java source code.

Maybe you want to define a native implementation for a java method.

Читайте также:  Php включить вывод warning

They seem to have a Java API, which you need to download and include in your classpath. You can’t include a C header file in java.

you cannot do it directly in java. You have to include the header files in your C program and use JNI to call the functions that you want to use.

Refer this : JNI reference

Java, write to file with headings, I want to have headings on this file. How Stack Overflow. About; Products For Teams; Stack Overflow java file writetofile. Share. Improve this …

Advantages/Disadvantages of Header Files

What are the advantages and disadvantages of using header files in a language like C or C++ verses a language like Java? I think classes should be designed from the outside in, so it is nice to have header files and not have to wade through implementation details. However, then again, each function declaration is duplicated across two files. If C and C++ were invented today would they use header files? Is this mechanism outdated or necessary?

Taken from a related blog post by Eric Lippert, who puts it very well:

I would have asked the equivalent question why does C++ need header files ? Header files seem like a huge potential point of failure; all the time I edit C++ code and change the signature of a method; if I forget to update the header file, then the code doesn’t compile and often gives some cryptic error message. Hopefully this large cost actually buys you something.

It buys the compiler writer one thing, and the user one thing.

What it buys the user is that you can compile each individual “cpp” file into a “obj” file independently, provided that you have all the necessary headers. All the information necessary to generate the bodies that are in a given cpp file can be gleaned from the set of headers. This means that the build system can recompile just those cpp files that changed, provided that no header changed.

What it buys the compiler writer is that every file can be compiled in “one pass”. Because every type and method declaration is processed before its first usage , the compiler can simply start from the top of the file, pull in all the included headers, and proceed from the top down, spitting out the obj file as it goes, never having to go back and revisit something its seen already.

This is in contrast to languages such as C# (about which the blog post is) and Java, which is a pretty close relative.

It’s still a good idea to separate interface and implementation. But it doesn’t have to be physical separation. In Java you can see the interface from javadoc. Java IDEs usually can display API structures, and they can fold blocks. There is no compelling reasons that require physical separation. C was invented decades ago so we don’t need to pick on it.

Briefly, in C or C++ header files allow different files to share common definitions or declarations.

In Java everything is an object, so there’s no concept of sharing anything except objects. Each object is one file; if you want to access the object, you import the file.

C Header Files, A header file is a source file that has the .h extension. Header files contain the function prototypes or function declaration, whereas the source code contains …

How to Add header row in text file using java

I have requirement to add header row in existing text file, is there any way to do it without affecting any other file format or structure.

Read the file, add the header and save the file:

try < BufferedReader in = new BufferedReader(new FileReader(yourFile)); String header = "Your Header"; String content = ""; while (in.ready()) < content += in.readLine(); >in.close(); String output = header + System.getProperty("line.separator") + content; FileWriter fstream = new FileWriter(YourOutputFile); BufferedWriter out = new BufferedWriter(fstream); out.write(output); out.close(); >catch (IOException e)

C/C++ header to java, 3. For C, you can use JNA. You do have to declare function signatures redundantly in Java, but do not have to write any glue code. JNA is very easy to …

Read file headers using java

I am trying to read file headers using java, I want to get file type(image, audio whatever). I have tried many examples but nothing seems to work. I tried preon and no luck, can some one tell me how to read file headers using preon or some other Java apis thanks.

 File f = new File(filepath); System.out.println("Mime Type of " + f.getName() + " is " + new MimetypesFileTypeMap().getContentType(f)); 

Use mime-util’s MagicMimeMimeDetector:

Why does Java not permit the use of headers as in C++, Source code written in Java is simple. There is no preprocessor, no #define and related capabilities, no typedef, and absent those features, no …

Источник

Parsing / reading C-Header files using Java

Another example of usage is: https://github.com/ricardojlrufino/cplus-libparser Library for metadata extraction (information about classes, methods, variables) of source code in C / C ++. It does a lot more than parsing header files, of course, but that shouldn’t hurt you.

Parsing / reading C-Header files using Java

You can use an existing C parser for Java. It does a lot more than parsing header files, of course, but that shouldn’t hurt you.

We use the parser from the Eclipse CDT project. This is an Eclipse plugin, but we sucessfully use it outside of Eclipse, we just have to bundle 3 JAR files of Eclipse with the parser JAR.

To use the CDT parser, start with an implementation of org.eclipse.cdt.core.model.ILanguage , for example org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage . You can call getTranslationUnit on it, passing the code and some helper stuff. A code file is represented by a org.eclipse.cdt.core.parser.FileContent instance (at least in CDT7, this seems to change a lot). The easiest way to create such an object is FileContent.createForExternalFileLocation(filename) or FileContent.create(filename, content) . This way you don’t need to care about the Eclipse IFile stuff, which seems to work only within projects and workspaces.

The IASTTranslationUnit you get back represents the whole AST of the file. All the nodes therein are instances of IASTSomething types, for example IASTDeclaration etc. You can implement your own subclass of org.eclipse.cdt.core.dom.ast.ASTVisitor to iterate through the AST using the visitor pattern. If you need further help, just ask.

The JAR files we use are org.eclipse.cdt.core.jar , org.eclipse.core.resources.jar , org.eclipse.equinox.common.jar , and org.eclipse.osgi.jar .

Edit : I had found a paper which contains source code snippets for this: «Using the Eclipse C/C++ Development Tooling as a Robust, Fully Functional, Actively Maintained, Open Source C++ Parser», but it is no longer available online (only as a shortened version).

Example using Eclipse CDT with only 2 jars.
— https://github.com/ricardojlrufino/eclipse-cdt-standalone-astparser
In the example has a class that displays the structure of the source file as a tree and another example making interactions on the api .

A detail is that with this api(Eclipse CDT Parser) you can do the parsing from a string in memory.

Another example of usage is:
https://github.com/ricardojlrufino/cplus-libparser
Library for metadata extraction (information about classes, methods, variables) of source code in C / C ++.

See file: https://github.com/ricardojlrufino/cplus-libparser/blob/master/src/main/java/br/com/criativasoft/cpluslibparser/SourceParser.java

As mentioned already, CDT is perfect for this task. But unlike described above I used it from within a plugin and was able to use IFiles. Then everything is so mouch easier. To get the «ITranslationUnit» just do:

ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create(myIFile); IASTTranslationUnit ias = tu.getAST(); 

I was i.e. looking for a special #define, so I could just:

ppc = ias.getAllPreprocessorStatements(); 

To get all the preprocessed code statements, each statement in array-element. Perfectly easy.

Parsing / reading C-Header files using Java, Just for more background (I’m just looking for a C-Header parser, not a solution for this particular problem): I have a text file containing data and a C-Header file explaining the structure. Both are a bit dynamic, so I don’t want to generate Java class files. example: #define TYPE1 typedef struct type1 < char name1[10]; char name2[5]; >#endif

C/C++ header to java

For C, you can use JNA. You do have to declare function signatures redundantly in Java, but do not have to write any glue code. JNA is very easy to use.

For C or C++, you can use SWIG. SWIG is a little more complex to use, but automatically generates Java wrappers for C++ classes. I’m enjoying it.

JNAerator does exactly that : it reads C/C++/ObjectiveC headers and outputs Java bindings that rely on BridJ (C/C++), JNA (C only) or Rococoa (ObjectiveC, uses JNA).

Looks like SWIG works with Java: http://www.swig.org/Doc2.0/Java.html

Maybe this isn’t exactly what you’re looking for, though, since you do have to add SWIG directives…

Is there anything in java like header files in C/C++, There’s no equivalent to header files in java, however you can import whole packages: import mypackage.*. Share. answered Oct 24, 2011 at 19:17. … Usage exampleimport mypackage.*Feedback

How to include Header File (.h) in Java

This might be more complicated than you think. The .h files are C language include files which usually describe the methods and data structures of a C-library. You would have to Java Native Interface (JNI) in order to include these libraries in your java code.

You have basically two options

There’re already java-bindings available.

You can’t include C/C++ headers into java source code.

Maybe you want to define a native implementation for a java method.

They seem to have a Java API, which you need to download and include in your classpath. You can’t include a C header file in Java.

C/C++ header to java, For C, you can use JNA. You do have to declare function signatures redundantly in Java, but do not have to write any glue code. JNA is …

Reading the header of a file in java

Maybe not the answer you wanted, but as you gave us very little information .

In unixoid systems (Linux, Mac, *BSD) you have the file command, that

tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests. The first test that succeeds causes the file type to be printed.

$ file linux-image-3.1.0-030100rc10-generic_3.1.0-030100rc10.201110200610_amd64.deb linux-image-3.1.0-030100rc10-generic_3.1.0-030100rc10.201110200610_amd64.deb: Debian binary package (format 2.0) 

Using Runtime.exec(. ) you could invoke that program and parse its output.

Edit 1:

To determine if a given file is a PNG:

import java.io.*; public class IsPng < public static void main(String . filenames) throws Exception < if(filenames.length == 0) < System.err.println("Please supply filenames."); return; >for(String filename : filenames) < if(isPng(new File(filename))) < System.out.println(filename + " is a png."); >else < System.out.println(filename + " is _not_ a png."); >> > private static final int MAGIC[] = new int[] < 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a >; private static boolean isPng(File filename) throws Exception < FileInputStream ins = new FileInputStream(filename); try < for(int i = 0; i < MAGIC.length; ++i) < if(ins.read() != MAGIC[i]) < return false; >> return true; > finally < ins.close(); >> > 
Edit 2:

Sometimes URLConnection.getContentType() works, too, for local files:

new File(name).toURI().toURL().openConnection().getContentType() 

But your comments sound like you have to implement the method by yourself, not using external programs (?).

You can try JFileChooser. Here is an example.

import java.io.*; import javax.swing.*; class GetFileType < public static void main(String[] args)< JFileChooser chooser = new JFileChooser(); File file = new File("Hello.txt"); String fileTypeName = chooser.getTypeDescription(file); System.out.println("File Type= "+fileTypeName); >> 

This will output Text Document. If it is a MP3 file passed then the output will be MP3 Format Sound.

Java equivalent of C header files, In C++ (or C) you commonly split the declarations that go into headers from the definitions that go in source files, and start the files with a …

Источник

Оцените статью