Programming Articles

Page 2384 of 2547

How to truncate a file in Java?

Swarali Sree
Swarali Sree
Updated on 20-Feb-2020 2K+ Views

The flush() method of the FileWriter class flushes the contents of the file. You can use this method to truncate a file.Exampleimport java.io.File; import java.io.FileWriter; public class FileTruncate { public static void main(String args[]) throws Exception { File file = new File("myData"); FileWriter fw = new FileWriter(file, false); fw.flush(); System.out.println("File truncated"); } }OutputFile truncated

Read More

How to get the number of capture groups in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

The following code gets the number of captured groups using Python regex in given stringExampleimport re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups())OutputThis gives the output3

Read More

How to write Python regular expression to get all the anchor tags in a webpage?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 257 Views

The following code extracts all tags in the given stringExampleimport re rex = re.compile(r'[\]') l = "this is text1 hi this is text2" print rex.findall(l)Output['', '']

Read More

How to use wildcard in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

The following code uses the Python regex .()dot character for wildcard which stands for any character other than newline.Exampleimport re rex = re.compile('th.s') l = "this, thus, just, then" print rex.findall(l)OutputThis gives the output['this', 'thus']

Read More

How to find all adverbs and their positions in a text using python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 512 Views

As per Python documentationIf one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or she would use finditer() in the following manner −>>> text = "He was carefully disguised but captured quickly by police." >>> for m in re.finditer(r"\w+ly", text): ... print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0))) 07-16: carefully 40-47: quickly

Read More

How to extract numbers from text using Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 20-Feb-2020 2K+ Views

If we want to extract all numbers/digits individually from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d', s) print resultOutput['1', '2', '3', '4', '5', '6', '7']If we want to extract groups of numbers/digits from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d+', s) print resultOutput['12345', '67']

Read More

How to capture file not found exception in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 20-Feb-2020 448 Views

While using FileInputStream, FileOutputStream, and RandomAccessFile classes, we need to pass the path of the file to their constructors. In case of a file in the specified path does not exist a FileNotFoundException is raised.Examplepublic class Sample { public static void main(String args[]) throws Exception { File file = new File("myFile"); FileInputStream fis = new FileInputStream(file); System.out.println("Hello"); } }OutputException in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified) ...

Read More

Can we define a class inside a Java interface?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 7K+ Views

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.Exampleinterface Library { void issueBook(Book b); void retrieveBook(Book b); public class Book { int bookId; String bookName; int issueDate; int returnDate; } } public class Sample implements Library ...

Read More

What is the Eclipse keyboard shortcut for "public static void main(String[] args) " in Java?

Ramu Prasad
Ramu Prasad
Updated on 20-Feb-2020 9K+ Views

To get public static void main(String[] args) line in eclipse without typing the whole line type main and press Ctrl + space then, you will get the option for the main method select it.

Read More

How to find package explorer in Java eclipse project?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 25K+ Views

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer.There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.

Read More
Showing 23831–23840 of 25,466 articles
Advertisements