问题
Is it possible to find instances of //
in a line read from a file into a byte array and then "snip" from //
to the end of the line out? I'm trying
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[8 * 1024];
int read;
while ((read = fis.read(buffer)) != -1)
{
for (int i = 0; i < read; i++)
{
if (buffer[i] == '//')
{
buffer = buffer[0:i];
}
}
}
but I'm getting Invalid character constant
at if (buffer[i] == '//')
on the '//'
part. Am I doing something wrong, or is this just not possible?
回答1:
Old-school solution
for (int i = 0; i < read-1; i++)
{
(if (buffer[i] == '/') && (buffer[i+1]== '/'))
{
buffer = buffer[0:i];
}
}
回答2:
'
and '
denote one character. Since //
are two characters this does not work. One has to differentiate between a character and a string. Thus you have to individually check both positions in the byte array to confirm there are two successive /
s.
来源:https://stackoverflow.com/questions/34420823/find-single-line-comments-in-byte-array