Create Directory Recursively Using Java

Samual Sam
Updated on 20-Feb-2020 09:49:22

358 Views

The java.io.File.mkdirs() creates the directory named by this abstract pathname, together with necessary and non-existent parent directories.ExampleLive Demoimport java.io.File; public class Main {    public static void main(String[] args) {       String directories = "D:\a\b\c\d ";       File file = new File(directories);       boolean result = file.mkdirs();       System.out.println("Status = " + result);    } }OutputStatus = true

CharMatcher Class in Java

AmitDiwan
Updated on 20-Feb-2020 09:25:44

259 Views

The CharMatcher class determines a true or false value for any Java char value, just as Predicate does for any Object.Sr.NoMethods & Description1CharMatcher and(CharMatcher other)Returns a matcher that matches any character matched by both this matcher and other.2static CharMatcher anyOf(CharSequence sequence)Returns a char matcher that matches any character present in the given character sequence.3boolean apply(Character character)Deprecated. Provided only to satisfy the Predicate interface; use matches(char) instead.4String collapseFrom(CharSequence sequence, char replacement)Returns a string copy of the input character sequence, with each group of consecutive characters that match this matcher replaced by a single replacement character.5int countIn(CharSequence sequence)Returns the number of matching ... Read More

Check the Existence of a File Using Java

Monica Mona
Updated on 20-Feb-2020 08:27:51

165 Views

The file class provides a method named exists() which returns true if the file specified in the current file object exists.ExampleLive Demoimport java.io.File; public class FileHandling {    public static void main(String args[]) {       File file = new File("samplefile");       if(file.exists()) {          System.out.println("Given file existed");       } else {          System.out.println("Given file does not existed");      }    } }OutputGiven file does not existed

List All Files in a Directory Using Java

karthikeya Boyini
Updated on 20-Feb-2020 08:21:45

1K+ Views

You can get the list of files in a directory −Create a directory object using the File class.Get the list of directories in it using the getName() method.Exampleimport java.io.File; public class FindingDirectories {    public static void main(String args[]) {       String dir ="C:/Users/Tutorialspoint/Desktop/movies";       File directory = new File(dir);       File[] fileList = directory.listFiles();       for(File file: fileList) {          System.out.println(file.getName());       }    } }OutputArundhati HD.mp4 Okka Ammai Tappa.mkv Padamati Sandhya Ragam.mp4

Difference Between System.out.println and System.out.print in Java

Sharon Christine
Updated on 20-Feb-2020 08:19:37

2K+ Views

The println() terminates the current line by writing the line separator string. The print() method just prints the given content.ExampleLive Demopublic class Sample {    public static void main(String args[]) {       System.out.println("Hello");       System.out.println("how are you");       System.out.print("Hello");       System.out.print("how are you");    } }OutputHello how are you Hellohow are you

Get Last 4 Characters of a String in Python

Malhar Lathkar
Updated on 20-Feb-2020 08:12:13

2K+ Views

The slice operator in Python takes two operands. First operand is the beginning of slice. The index is counted from left by default. A negative operand starts counting from end. Second operand is the index of last character in slice. If omitted, slice goes upto end.We want last four characters. Hence we count beginning of position from end by -4 and if we omit second operand, it will go to end.>>> string = "Thanks. I am fine" >>> string[-4:] 'fine'

Assign a Reference to a Variable in Python

Malhar Lathkar
Updated on 20-Feb-2020 08:11:01

1K+ Views

Concept of variable in Python is different from C/C++. In C/C++, variable is a named location in memory. Even if value of one is assigned to another, it creates a copy in another location.int x=5; int y=x;For example in C++, the & operator returns address of the declared variable.cout

Unpack String of Integers to Complex Numbers in Python

Malhar Lathkar
Updated on 20-Feb-2020 08:09:49

226 Views

A string contains two integers separated by comma. It is first split in a list of two strings having digits.>>> s="1,2".split(",") >>> s ['1', '2']Two items are then converted to integers and used as arguments for complex() function>>> complex(int(s[0]), int(s[1])) (1+2j)This results in unpacking of string of integers in a complex number

Iterate Through a Python List of Tuples

Malhar Lathkar
Updated on 20-Feb-2020 08:07:37

8K+ Views

Easiest way is to employ two nested for loops. Outer loop fetches each tuple and inner loop traverses each item from the tuple. Inner print() function end=’ ‘ to print all items in a tuple in one line. Another print() introduces new line after each tuple.ExampleL=[(1,2,3), (4,5,6), (7,8,9,10)] for x in L:   for y in x:     print(y, end=' ')   print()Output1 2 3 4 5 6 7 8 9 10

Read and Write Unicode UTF-8 Files in Python

Rajendra Dharmkar
Updated on 20-Feb-2020 07:59:19

29K+ Views

The io module is now recommended and is compatible with Python 3's open syntax: The following code is used to read and write to unicode(UTF-8) files in PythonExampleimport io with io.open(filename,'r',encoding='utf8') as f:     text = f.read() # process Unicode text with io.open(filename,'w',encoding='utf8') as f:     f.write(text)

Advertisements