Created
December 28, 2016 14:58
-
-
Save Banafasto/666a212c525f146b2a3a812282e281cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.kudr641; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.Scanner; | |
public class Lessons7Example1 { | |
public static void main(String[] args) { | |
File file = new File("text.txt"); | |
Scanner scanner = new Scanner(System.in); | |
try { | |
file.createNewFile(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("Hello in PetrNotePad v0.1 enter your text if you want exit enter *exit*"); | |
String text = ""; | |
while (true) { | |
String line = scanner.nextLine(); | |
text += line + System.lineSeparator(); | |
if (line.equals("exit")) { | |
break; | |
} | |
} | |
saveTextFile(text, file); | |
scanner.close(); | |
} | |
static void saveTextFile(String text, File file) { | |
try (PrintWriter pw = new PrintWriter(file)) { | |
pw.println(text); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.kudr641; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.Arrays; | |
import java.util.Random; | |
import java.util.Scanner; | |
public class Lessons7Example2 { | |
public static void main(String[] args) { | |
File file = new File("matrix.txt"); | |
try { | |
file.createNewFile(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
int [][] matrix = createArray(); | |
String matrixString = ""; | |
for(int i = 0; i < matrix.length; i += 1){ | |
String line = Arrays.toString(matrix[i]) + System.lineSeparator(); | |
matrixString += line; | |
} | |
saveTextFile(matrixString, file); | |
} | |
static int[][] createArray(){ | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Enter size matrix: "); | |
int size = scanner.nextInt(); | |
Random random = new Random(); | |
int[][] matrix = new int[size][size]; | |
for(int i = 0; i < matrix.length; i += 1){ | |
for(int j = 0; j < matrix[i].length; j += 1){ | |
matrix[i][j] = random.nextInt(10); | |
} | |
} | |
return matrix; | |
} | |
static void saveTextFile(String text, File file) { | |
try (PrintWriter pw = new PrintWriter(file)) { | |
pw.println(text); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmail.kudr641; | |
import java.io.File; | |
import java.util.Scanner; | |
public class Lessons7Example3 { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("The distribution directory:"); | |
String directory = scanner.nextLine(); | |
scanner.close(); | |
directoryList(directory); | |
} | |
static void directoryList(String directory) { | |
File file = new File(directory); | |
File[] list = file.listFiles(); | |
for (int i = 0; i < list.length; i += 1) { | |
if(list[i].isDirectory()){ | |
System.out.println(list[i]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment