Java implementation
import ; import ; import ; import ; import ; import ; public class JavaFile { public static void main(String[] args) { try { // read file content from file StringBuffer sb= new StringBuffer(""); FileReader reader = new FileReader("c://"); BufferedReader br = new BufferedReader(reader); String str = null; while((str = ()) != null) { (str+"/n"); (str); } (); (); // write string to file FileWriter writer = new FileWriter("c://"); BufferedWriter bw = new BufferedWriter(writer); (()); (); (); } catch(FileNotFoundException e) { (); } catch(IOException e) { (); } } }
C++ implementation
#include<string> #include<iostream> #include<> #include<fstream> int main() { std::string file_name="123"; std::ifstream fin(file_name.c_str()); std::string textline[3]; for(int i=0;i<3;++i) getline(fin,textline[i],'\n');//After the line break ends readingfor(int i=0;i<3;++i) std::cout<<textline[i]<<'\n'; return 0; }
PHP implementation
<?php /** * Read the file by line * @param string $filename */ function readFileByLine ($filename) { $fh = fopen($filename, 'r'); while (! feof($fh)) { $line = fgets($fh); echo $line; } fclose($fh); } // test $filename = "/home/wzy/test/"; readFileByLine($filename);
Implementation of C language
#include <> #include <> #include <> #define LEN 1024 int main(void) { char filename[LEN], buf[LEN]; FILE *fp; int len; scanf("%s", filename); fp = fopen(filename, "r"); if (fp == NULL) exit(-1); while (fgets(buf, LEN, fp) != NULL) { len = strlen(buf); buf[len - 1] = '\0'; // Remove line breaks printf("%s\n", buf); } return 0; }