Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a rough implementation that guesses the line ending type based on a simple majority and falls back on unknown in a worst-case scenario:</p> <pre><code>import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.EnumMap; import java.util.Map; import java.util.Scanner; class LineEndings { private enum ExitState { SUCCESS, FAILURE; } public enum LineEndingType { DOS("Windows"), MAC("Mac OS Classic"), UNIX("Unix/Linux/Mac OS X"), UNKNOWN("Unknown"); private final String name; private LineEndingType(String name) { this.name = name; } public String toString() { if (null == this.name) { return super.toString(); } else { return this.name; } } } public static void main(String[] arguments) { ExitState exitState = ExitState.SUCCESS; File inputFile = getInputFile(); if (null == inputFile) { exitState = ExitState.FAILURE; System.out.println("Error: No input file specified."); } else { System.out.println("Determining line endings for: " + inputFile.getName()); try { LineEndingType lineEndingType = getLineEndingType(inputFile); System.out.println("Determined line endings: " + lineEndingType); } catch (java.io.IOException exception) { exitState = ExitState.FAILURE; System.out.println("Error: " + exception.getMessage()); } } switch (exitState) { case SUCCESS: System.exit(0); break; case FAILURE: System.exit(1); break; } } private static File getInputFile() { File inputFile = null; Scanner stdinScanner = new Scanner(System.in); while (true) { System.out.println("Enter the input file name:"); System.out.print("&gt;&gt; "); if (stdinScanner.hasNext()) { String inputFileName = stdinScanner.next(); inputFile = new File(inputFileName); if (!inputFile.exists()) { System.out.println("File not found.\n"); } else if (!inputFile.canRead()) { System.out.println("Could not read file.\n"); } else { break; } } else { inputFile = null; break; } } System.out.println(); return inputFile; } private static LineEndingType getLineEndingType(File inputFile) throws java.io.IOException, java.io.FileNotFoundException { EnumMap&lt;LineEndingType, Integer&gt; lineEndingTypeCount = new EnumMap&lt;LineEndingType, Integer&gt;(LineEndingType.class); BufferedReader inputReader = new BufferedReader(new FileReader(inputFile)); LineEndingType currentLineEndingType = null; while (inputReader.ready()) { int token = inputReader.read(); if ('\n' == token) { currentLineEndingType = LineEndingType.UNIX; } else if ('\r' == token) { if (inputReader.ready()) { int nextToken = inputReader.read(); if ('\n' == nextToken) { currentLineEndingType = LineEndingType.DOS; } else { currentLineEndingType = LineEndingType.MAC; } } } if (null != currentLineEndingType) { incrementLineEndingType(lineEndingTypeCount, currentLineEndingType); currentLineEndingType = null; } } return getMostFrequentLineEndingType(lineEndingTypeCount); } private static void incrementLineEndingType(Map&lt;LineEndingType, Integer&gt; lineEndingTypeCount, LineEndingType targetLineEndingType) { Integer targetLineEndingCount = lineEndingTypeCount.get(targetLineEndingType); if (null == targetLineEndingCount) { targetLineEndingCount = 0; } else { targetLineEndingCount++; } lineEndingTypeCount.put(targetLineEndingType, targetLineEndingCount); } private static LineEndingType getMostFrequentLineEndingType(Map&lt;LineEndingType, Integer&gt; lineEndingTypeCount) { Integer maximumEntryCount = Integer.MIN_VALUE; Map.Entry&lt;LineEndingType, Integer&gt; mostFrequentEntry = null; for (Map.Entry&lt;LineEndingType, Integer&gt; entry : lineEndingTypeCount.entrySet()) { int entryCount = entry.getValue(); if (entryCount &gt; maximumEntryCount) { mostFrequentEntry = entry; maximumEntryCount = entryCount; } } if (null != mostFrequentEntry) { return mostFrequentEntry.getKey(); } else { return LineEndingType.UNKNOWN; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload