Category Archives: Programming 程式碼

Linux CMD

Command
Create an empty text file named index.txt
touch index.txt

Create and write file
cat > index.txt//Wirte abcd contentCtrl + D

Copy File
cp -r index.txt ./floder_path

Zip File
zip index.zip index.txt

UnZip File
zip index.zip index.txt

tar File (zip)
tar -zcvf test3.tar.gz test3

tar File (unzip)
tar -xvzf test3.tar.gz test3

Nano
Search keyword(s) [Next]
Ctrl + WEnter

Search keyword(s) [Backwards]
Ctrl + WAlt + W

Undo
Alt + U

Redo
Alt + E

Save
Ctrl +

Filter file name in Java

import java.io.File;
import java.io.*;

public class ReadFile {
	public static void main(String[] args) {
		try {
			// Create a file object
			File f = new File("C:/OGSAN/N42/1000/2000");

			// Create a FilenameFilter
			FilenameFilter filter = new FilenameFilter() {
				public boolean accept(File f, String name) {
					return name.startsWith("04");
				}
			};

			// Get all the names of the files present
			// in the given directory
			// and whose names start with "12"
			File[] files = f.listFiles(filter);

			// Display the names of the files
			for (int i = 0; i < files.length; i++) {
				System.out.println(files[i].getName());
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

	}
}

List files in a directory (Java)

import java.io.File;
import java.util.Arrays;

public class ReadFile {
	public static void main(String[] args)
	  {
	    // create a file that is really a directory
	    File aDirectory = new File("C:/OGSAN/N42/1000/2000");

	    // get a listing of all files in the directory
	    String[] filesInDir = aDirectory.list();

	    // sort the list of files (optional)
	    // Arrays.sort(filesInDir);

	    // have everything i need, just print it now
	    for ( int i=0; i<filesInDir.length; i++ )
	    {
	      System.out.println( "file: " + filesInDir[i] );
	    }
	  }
}

OUTPUT:
file: 0401.txt
file: 0402.txt
file: 0501.txt
file: 0502.txt