Erinevus lehekülje "JavaPython:Failioperatsioonid" redaktsioonide vahel
Mine navigeerimisribale
Mine otsikasti
(Uus lehekülg: '{{JavaPython-sisukord}}') |
|||
1. rida: | 1. rida: | ||
{{JavaPython-sisukord}} | {{JavaPython-sisukord}} | ||
+ | |||
+ | == Näide == | ||
+ | |||
+ | {| | ||
+ | !Java | ||
+ | !Python | ||
+ | |- | ||
+ | |<syntaxhighlight lang="java" line="1" > | ||
+ | //get current directory | ||
+ | File dir = new File("."); | ||
+ | File file = new File(dir.getCanonicalPath() | ||
+ | + File.separator + "Code.txt"); | ||
+ | FileInputstream fis = new FileInputStream(file); | ||
+ | |||
+ | //Construct the BufferedReader object | ||
+ | BufferedReader br = new BufferedReader | ||
+ | (new InputStreamReader(fis)); | ||
+ | String line = null; | ||
+ | while ((line = br.readline())!= null) { | ||
+ | //process each line, here we count the empty lines | ||
+ | if (line.trim().length() == 0) { | ||
+ | } | ||
+ | } | ||
+ | //Do not forget to close the BufferedReader | ||
+ | br.close() | ||
+ | </syntaxhighlight> | ||
+ | |<syntaxhighlight lang="python" line="2" > | ||
+ | file = open("some_path_to_file") | ||
+ | print file.read(); | ||
+ | </syntaxhighlight> | ||
+ | |} |
Redaktsioon: 2. veebruar 2016, kell 10:42
Java vs Python |
|
Näide
Java | Python |
---|---|
<syntaxhighlight lang="java" line="1" >
//get current directory File dir = new File("."); File file = new File(dir.getCanonicalPath() + File.separator + "Code.txt"); FileInputstream fis = new FileInputStream(file); //Construct the BufferedReader object BufferedReader br = new BufferedReader (new InputStreamReader(fis)); String line = null; while ((line = br.readline())!= null) { //process each line, here we count the empty lines if (line.trim().length() == 0) { } } //Do not forget to close the BufferedReader br.close() </syntaxhighlight> |
<syntaxhighlight lang="python" line="2" >
file = open("some_path_to_file") print file.read(); </syntaxhighlight> |