using System; using System.IO; namespace nama_nim_filetxt { class Program { public static void tulis_file() { Console.WriteLine(" Text File I/O "); Console.WriteLine(" Menggunakan Array 2 Dimensi "); Console.WriteLine(); Console.Write("Masukkan Jumlah Mahasiswa : "); int x = Convert.ToInt32(Console.ReadLine()); string [,] dataArray1 = new string[x,1]; string [,] dataArray2 = new string[x,1]; for(int i=0; i < x ; i++) { for(int j=0; j < 1; j++) { Console.Write("NIM Mahasiswa ke – " + (i+1).ToString() + " = "); dataArray1[i,j]=Console.ReadLine(); Console.Write("Nama Mahasiswa ke – " + (i+1).ToString()+" = "); dataArray2[i,j]=Console.ReadLine(); } } string data=""; for(int i=0 ; i < x ; i++) { for(int j=0; j < 1 ; j++) { data +="NIM = " +dataArray1[i,j]+" # "+"Nama = "+dataArray2[i,j]+"\r\n"; } } StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine(data); sw.Close(); Console.WriteLine("Sukses menuliskan data ke file test.txt"); } public static void baca_file() { StreamReader sr = new StreamReader("test.txt"); string data = sr.ReadToEnd(); sr.Close(); Console.WriteLine("Baca file.txt"); Console.WriteLine(data); } public static void Main(string[] args) { tulis_file(); baca_file(); Console.ReadKey(true); } } }
C++ :
#include#include using namespace std; int main() { ifstream input; input.open("text.txt"); cout << " Text File I/O " << endl; cout << " Dengan Array 2 dimensi " << endl; cout << " " << endl; int x ; cout << "Masukkan Jumlah Mahasiswa : "; cin >> x ; string dataArray1 [x][1]; string dataArray2 [x][1]; for(int i=0; i > dataArray1[i][j]; cout << "Nama Mahasiswa ke " << (i+1) << " = "; cin >> dataArray2[i][j]; } } cout << " " << endl; string data=""; for(int i=0 ; i < x ; i++) { for(int j=0 ; j < 1 ; j++) { cout << "NIM = "<< dataArray1[i][j]<< " # "<< "Nama = " << dataArray2[i][j]<< "\r\n"; } } input.close(); ofstream output; output.open("text.txt"); for(int i=0; i < x ; i++) { for(int j=0; j < 1 ; j++) { output << "NIM = " << dataArray1[i][j]<< " # "<< "Nama = " << dataArray2[i][j]<< "\r\n"; } cout << " " << endl; } return 0; }
Java :
# MAIN CLASS package matrix; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class matrix1 { public void tulis_file () { File output=new File("output.txt"); FileWriter writer; Scanner Scanner; Scanner scn = new Scanner(System.in); try{ writer=new FileWriter(output); BufferedWriter buf=new BufferedWriter(writer); System.out.print("Jumlah Mahasiswa : "); int jumlah = Integer.parseInt(new Scanner(System.in).nextLine()); String [][] dataArray = new String[jumlah][1]; String [][] data1 = new String[jumlah][1]; for(int i = 0; i < jumlah ; i++) //Baris { for(int j = 0; j < 1 ; j++) //Kolom { System.out.print("Nama Mahasiswa Ke "+(i+1)+" : "); dataArray[i][j] =scn.next(); System.out.print("NIM Mahasiswa Ke "+(i+1)+" : "); data1[i][j] =scn.next(); } } String data = ""; for(int i = 0; i < jumlah ; i++) { for(int j = 0; j < 1 ; j++) { data +="nama : " + dataArray[i][j] +"\r\n" +"nim : "+data1[i][j]+ "\r\n"; } System.out.println(); } buf.write(data); buf.flush(); buf.close(); System.out.println("sukses menyimpan output.txt!!"); }catch(IOException e){ System.err.println("ada yg error"); } } public void baca_file() throws FileNotFoundException { FileReader reader; File file=new File("output.txt"); Scanner read = new Scanner(file); while(read.hasNextLine()){ String line=read.nextLine(); System.out.println(line); } System.out.println(" Baca File output.txt!!!"); } } # MAIN PROGRAM package matrix; import java.io.FileNotFoundException; public class Matrix { public static void main(String[] args) throws FileNotFoundException { matrix1 tft = new matrix1 (); tft.tulis_file(); tft.baca_file(); } }
Terima kasih....
Semoga bermanfaat...
0 komentar:
Posting Komentar