1. Set window size

    The size of a JavaFX application window can be set in two places.

    It can be set in either the Stage or the Scene object using the width and height variables.

  2. Delete a file or directory

    Deleting a file or directory in Java is pretty simple. Create a File object and call the .delete() method on it.

    import java.io.File;
     
    File file = new File(filePath);
    file.delete();

    The .delete() method:
    Returns true if the file/directory is deleted successfully
    Returns false if the file/directory is not deleted (ie. file not found, file is open)
    Throws a SecurityException if the application has insufficient permissions to delete the file/directory.

  3. Get the current URL

    If you already know some information about the URL you can use that information to replace the respective part.

    Start with an empty path

    <?php
    $path 
    "";
    ?>

    Check for the protocol (ie. http, cgi)
    If you know the protocol is http you can replace this accordingly.

    <?php
    // SERVER_PROTOCOL is in the form "protocol/rev#", this strips the revision
    $path .= strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/");
    ?>

    Check if secure (HTTPS)

  4. Hello World GUI

    This is an example of a basic JavaFX application.

  5. Read a text file using readLine()

    One of the simplest ways to read in a text file in Java is to use the readLine() method to read one line at a time. In this example the file is opened with a FileReader, buffered with a BufferedReader, and then read in using the readLine() method.

    import java.io.*;
     
    public class ReadTextFile {
     
        public static void main(String[] args) {
     
            StringBuilder contents = new StringBuilder();
     
            try {
                BufferedReader input =  new BufferedReader(new FileReader("C:\\Temp\\myFile.txt"));

  6. Convert a string to an integer using scanf and sscanf

    int  scanf ( const char * format, ... );
    int sscanf ( const char * str, const char * format, ...);

    Getting input from keyboard (stdin)

    // NOTE: this uses scanf
    #include <stdio.h>
    int main()
    {
         int i;
         printf( "Enter an integer: " );
         if( scanf( "%d", &i ) <=0 )
         {
              printf ("ERROR");
         }
         else
         {
              printf ("The integer value is: %d.",i);
         }
         return 0;
    }

Syndicate content