Programming Articles - Page 2022 of 3366

What is the importance of jmod format in Java 9?

raja
Updated on 09-Apr-2020 18:15:29

1K+ Views

Java 9 has introduced a new format called "jmod" to encapsulate modules. The jmod files can be designed to handle more content types than jar files. It can also package local codes, configuration files, local commands, and other types of data. The "jmod" format hasn't support at runtime and can be based on zip format currently. The jmod format can be used at both compile and link time and can be found in JDK_HOME\jmods directory, where JDK_HOME is a directory. The files in jmod format have a ".jmod" extension.Java 9 comes with a new tool called jmod, and it is located in the ... Read More

What are the different "/types" commands in JShell in Java 9?

raja
Updated on 09-Apr-2020 14:57:27

486 Views

JShell tool has introduced in Java 9 version. It is also called a REPL(Read-Evaluate-Print-Loop) tool that allows us to execute Java code and getting immediate results. We need to list out the declared types like class, interface, enum, and etc by using the "/types" command.Below are the different "/types" commands in JShell./types /types [ID] /types [Type_Name] /types -start /types -all/types: This command lists all active types (class, interface, enum) created in JShell./types [ID]: This command displays the type corresponding to the id [ID]./types [Type_Name]: This command displays the type corresponding to [Type_Name]./types -start: This command allows us to list the types that ... Read More

When to use the ServiceLoader class in a module in Java 9?

raja
Updated on 09-Apr-2020 12:22:21

295 Views

Java has a ServiceLoader class from java.util package that can help to locate service providers at the runtime by searching in the classpath. For service providers defined in modules, we can look at the sample application to declare modules with service and how it works.For instance, we have a "test.app" module that we need to use Logger that can be retrieved from System.getLogger() factory method with the help of LoggerFinder service.module com.tutorialspoint.test.app {    requires java.logging;    exports com.tutorialspoint.platformlogging.app;    uses java.lang.System.LoggerFinder; }Below is the test.app.MainApp class:package com.tutorialspoint.platformlogging.app; public class MainApp {    private static Logger LOGGER = System.getLogger();    public static void ... Read More

How to prevent multiple inserts when submitting a form in PHP?

AmitDiwan
Updated on 09-Apr-2020 13:40:58

2K+ Views

PHP session can be used to prevent multiple inserts when submitting a form. PHP session sets a session variable (say $_SESSION['posttimer']) that sets the current timestamp on POST. Before processing the form in PHP, the $_SESSION['posttimer'] variable is checked for its existence and checked for a specific timestamp difference (say 2 or 3 seconds). This way, those insertions that are actually duplicates can be identified and removed.Simple form −// form.html         The reference to ‘my_session_file.php’ in the above will have the below lines of code −Exampleif (isset($_POST) && !empty($_POST)) {    if (isset($_SESSION['posttimer'])) {     ... Read More

PHP Casting Variable as Object type in foreach Loop

AmitDiwan
Updated on 09-Apr-2020 11:44:45

1K+ Views

This depends on the IDE that is being used. For example, Netbeans and IntelliJ can enable the usage of @var in a comment −/* @var $variable ClassName */ $variable->This way, the IDE would know that the ‘$variable’ is a class of the ClassName after the hint ‘->’ is encountered.In addition, an @return annotation can be created with a method that specifies that the return type will be an array of ClassName objects. This data can be accessed using a foreach loop that fetches the values of the objects −function get_object_type() {    return $this->values; } foreach( $data_object-> values as $object_attribute ... Read More

Sort php multidimensional array by sub-value in PHP

AmitDiwan
Updated on 09-Apr-2020 11:43:24

380 Views

The ‘usort’ function can be used to sort multidimensional arrays in PHP. It sorts an array based on a user-defined criteria −Example Live DemoOutputThis will produce the following output −2 4 63 81An array with 4 elements is declared and this array is passed to the usort function, as well as calling the user-defined ‘my_sort’ function on the elements to make sure that the sorting takes place is ascending order.

In PHP, how can I add an object element to an array?

AmitDiwan
Updated on 09-Apr-2020 11:41:50

3K+ Views

The code is as follows −Example$object = new stdClass(); $object->name = "My name"; $myArray[] = $object;OutputThis will produce the following output −Suppose myArray already contains ‘a’ and ‘c’, the value of “My name” will be added to it. It becomes Array {    a:0, c:1, “My name”:2 }The object is created and then it is pushed to the end of the array (that was previously present).Alternative$myArray[] = (object) ['name' => 'My name'];

Detect language from string in PHP

AmitDiwan
Updated on 09-Apr-2020 11:40:36

1K+ Views

The language can’t be detected from the character type. There are other ways, but they don’t guarantee complete accuracy. The ‘TextLanguageDetect Pear Package’ can be used with decent accuracy. Below is a sample code for the same −Examplerequire_once 'Text/LanguageDetect.php'; $l = new Text_LanguageDetect(); $result = $l->detect($text, 4); if (PEAR::isError($result)) {    echo $result->getMessage(); } else {    print_r($result); }OutputThis will produce the following output −Array (    [german] => 0.407037037037    [dutch] => 0.288065843621    [english] => 0.283333333333    [danish] => 0.234526748971 )It is easy to use and has 52 language databases. But the downside is that the Eastern Asian languages can’t be detected using this package.

How to read only 5 last line of the text file in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:39:18

651 Views

To read only 5 last lines of the text file, the code is as follows −Example$file = file("filename.txt"); for ($i = max(0, count($file)-6); $i < count($file); $i++) {    echo $file[$i] . ""; }OutputThis will produce the following output −Given that the file has more than 5 lines of text, the last 5 lines of the text file will be displayed.The file is opened and the number of lines in the file are counted, and beginning from the last line, 5 lines are read.

How to download large files through PHP script?

AmitDiwan
Updated on 09-Apr-2020 11:37:51

3K+ Views

To download large files through PHP script, the code is as follows −ExampleOutputThis will produce the following output −The large file will be downloaded.The function ‘readfile_chunked’ (user defined) takes in two parameters- the name of the file and the default value of ‘true’ for the number of bytes returned meaning that large files have been successfully downloaded. The variable ‘chunksize’ has been declared with the number of bytes per chunk that needs to be read. The ‘buffer’ variable is assigned to null and the ‘cnt’ is set to 0. The file is opened in binary read mode and assigned the ... Read More

Advertisements