Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
69 lines (50 loc) · 2.08 KB

File metadata and controls

69 lines (50 loc) · 2.08 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* PROGRAMMA CHE AGGIUNGE IL CARATTERE SCELTO DALL'UTENTE ALL'INIZIO DI OGNI RIGA DI UN FILE
* (C) Antonio Maulucci 2017
*
* Il file di origine chiamato f e' definito nel codice e puo' essere modificato.
* Questo programma non gestisce eccezioni ma utilizza quelle di default di Java.
*/
package com.antomau.putcharatstartline;
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class PutCharAtStartLine {
public static void main(String args[]) throws FileNotFoundException
{
//File f = new File(args[1]); //new file
File f = new File("text.txt");
Scanner sc = new Scanner(f); //new scanner for file
Scanner in = new Scanner(System.in); //scanner for user inputs
System.out.println("What char do ypu want to put at every start-line: ");
char c = in.next().charAt(0); //take the char from user
newfile(c, sc);
sc.close(); //close the file's scanner
in.close(); //close the stdin scanner
System.out.println("Done! '" + c + "' added at every start of line!");
}
/**
* Questo metodo inserisce ad ogni inizio riga il carattere definito dall'utente
* @param c Il carattere da inserire all'inizio di ogni riga
* @param sc Lo scanner del file da leggere
*/
public static void newfile(char c, Scanner sc) throws FileNotFoundException
{
PrintWriter outfile = new PrintWriter("nfile.txt"); //file di destinazione (nfile.txt)
while (sc.hasNextLine())
{
String cur = sc.nextLine(); //cur e' la stringa corrente all'interno del file
String newstr = c + " " + cur; //newstr = carattere + spazio + riga originaria
/*
* per evitare che alla fine del file vi sia un'andata a capo indesiderata
* effettuo un controllo per verificare se c'e' o meno un'ulteriore riga nel file:
* Se vi e' un altra tiga aggiungo un andata a capo
* altrimenti no
*/
if (sc.hasNextLine()==true)
newstr += '\n';
outfile.print(newstr); //print no println: per non aggiungere andate a capo le quali sono controllate manualmente
}
outfile.close(); //chiusura del file di destinazione
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.